【发布时间】:2021-04-15 13:47:59
【问题描述】:
我正在使用 asp.net 核心 MVC 项目,但在尝试从视图 (cshtml) 中获取模型的 Id API 时遇到问题。 (请注意使用 Postman 测试时 getbyid API 有效)
我的模特:
public class Movie
{
[Key]
public int Id { get; set; }
public string Title { get; set; }
public string Director { get; set; }
public DateTime DateReleased { get; set; }
[Range(0, 9)]
public int Rating { get; set; }
public string Image { get; set; }
}
我的控制器方法:
namespace MovieTheatreManagement.Controllers
{
[Route("api/Movie")]
[ApiController]
public class MovieController : Controller
{
#region Fields
private readonly IMovieRepository _movieRepository;
private readonly IMapper _mapper;
private readonly ILogger _logger;
#endregion Fields
#region Constructors
public MovieController(IMovieRepository movieRepository, IMapper mapper, ILogger<MovieController> logger)
{
_movieRepository = movieRepository;
_mapper = mapper;
_logger = logger;
}
#endregion Constructors
#region HtmlRequests
[HttpGet("/")]
public IActionResult Index()
{
return View(_movieRepository.GetAllMovies().ToList().OrderBy(m => m.Id));
}
public ActionResult Create()
{
return View();
}
[HttpGet("Delete")]
[Route("Delete/{id}")]
public ActionResult Delete([FromRoute] int id)
{
var movie = _movieRepository.GetMovieById(id);
return View(movie);
}
#endregion HtmlRequests
[HttpGet("/All")]
public ActionResult<IEnumerable<GetModels>> GetAllMovies()
{
var movieItems = _movieRepository.GetAllMovies();
// var newitems = Convert.FromBase64String();
return Ok(_mapper.Map<IEnumerable<GetModels>>(movieItems));
}
[HttpPost]
public ActionResult Create([FromForm]CreateModels movieRequest)
{
//CreateModels movieRequest = new CreateModels();
_logger.LogInformation("hi I am create method");
var movie = _mapper.Map<Movie>(movieRequest);
_movieRepository.Create(movie);
_movieRepository.SaveChanges();
var command = _mapper.Map<GetModels>(movie);
return RedirectToAction("Index");
//CreatedAtRoute(nameof(GetMovieById), new {Id = command.Id}, command);
}
[HttpPost("id")]
public ActionResult ApplyDelete([FromRoute(Name = "id")]int id)
{
var command = _movieRepository.GetMovieById(id);
if (command == null)
{
return NotFound();
}
_movieRepository.DeleteMovie(command);
_movieRepository.SaveChanges();
return RedirectToAction("Index");
}
我的存储库(这是我调试后得到 id null 的问题)
public Movie GetMovieById(int id)
{
return _context.Movies.FirstOrDefault(item => item.Id == id);
}
查看:
@model IEnumerable<MovieTheatreManagement.Models.Movie>
@{
ViewBag.Title = "Index";
}
<h2>Movie Theatre Management</h2>
<p>
@Html.ActionLink("Create New Movie", "Create", "Movie", new{}, new { @class = "btn btn-default" })
</p>
<p>
@using (Html.BeginForm())
{
<div>
@Html.TextBox("txtTitle", "")
<input type="submit" value="Filter by Title" class="btn btn-default" />
</div>
}
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.Id)
</th>
<th>
@Html.DisplayNameFor(model => model.Title)
</th>
<th>
@Html.DisplayNameFor(model => model.DateReleased)
</th>
<th>
@Html.DisplayNameFor(model => model.Director)
</th>
<th>
@Html.DisplayNameFor(model => model.Rating)
</th>
<th>
@Html.DisplayNameFor(model => model.Image)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Id)
</td>
<td>
@Html.DisplayFor(modelItem => item.Title)
</td>
<td>
@Html.DisplayFor(modelItem => item.DateReleased)
</td>
<td>
@Html.DisplayFor(modelItem => item.Director)
</td>
<td>
@Html.DisplayFor(modelItem => item.Rating)
</td>
<td>
<img width="100px" height="100px" src=@Html.DisplayFor(modelItem => item.Image) />
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.Id }) |
@Html.ActionLink("Delete Movie", "Delete", "Movie" ,new { id = item.Id })
</td>
</tr>
}
</table>
删除页面:
@model MovieTheatreManagement.Models.Movie
@{
ViewBag.Title = "Delete";
}
<h2>Delete</h2>
<h3>Are you sure you want to delete this?</h3>
<div>
<h4>Movie</h4>
<hr />
<dl class="dl-horizontal">
<dt>
@Html.DisplayNameFor(model => model.Title)
</dt>
<dd>
@Html.DisplayFor(model => model.Title)
</dd>
<dt>
@Html.DisplayNameFor(model => model.DateReleased)
</dt>
<dd>
@Html.DisplayFor(model => model.DateReleased)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Director)
</dt>
<dd>
@Html.DisplayFor(model => model.Director)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Rating)
</dt>
<dd>
@Html.DisplayFor(model => model.Rating)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Image)
</dt>
<dd>
@Html.DisplayFor(model => model.Image)
</dd>
</dl>
@using (Html.BeginForm(actionName:"ApplyDelete", controllerName:"Movie", new { id = Model.Id } , FormMethod.Post)) {
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Delete" class="btn btn-default"/>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
</div>
注意:我认为 ActionLink 中的 routeValues 没有为我提供所需的 ID
【问题讨论】:
-
什么 API 不工作?
-
Delete API 因为我无法获取 ID,调试后它总是为 null。
-
您要采取什么行动?你有 2 个删除,并且都有 id。
-
您也可以发布您的控制器吗?
-
关于第一个问题,我有 2 个删除,因为当用户单击我的索引页面中的删除链接时,我想转发到我的删除页面(我忘了提及,但我会在编辑中包含)。第二个问题,我当然会发布我的控制器
标签: c# asp.net .net asp.net-mvc