我仍然是 ASP.net MVC3 的新手,我正在尝试创建一个可以添加到用户的可选项目列表。所以 Get 方法看起来像
public ViewResult AddFavourite(int id)
{
ViewBag.UserId = id;
var movies = (from m in db.Movies
select new SelectableMovie() { Movie = m });
return View(movies.ToList());
}
并且视图看起来像
@using MovieManager.Models
@model List<SelectableMovie>
@using (Html.BeginForm("AddFavourite", "Users",
new { userId = ViewBag.UserId, movies = Model }, FormMethod.Post))
{
<table>
<tr>
<th>
Add
</th>
<th>
Title
</th>
<th>
Description
</th>
<th>
ReleaseDate
</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.CheckBoxFor(modelItem => item.Selected)
</td>
<td>
@Html.DisplayFor(modelItem => item.Movie.Title)
</td>
<td>
@Html.DisplayFor(modelItem => item.Movie.Description)
</td>
<td>
@Html.DisplayFor(modelItem => item.Movie.ReleaseDate)
</td>
</tr>
}
</table>
<p>
<input type="submit" value="Add Favourites" />
</p>
}
这会按预期显示列表,但是当我回发到控制器时,我得到了一个 userId,但 movies 参数是一个空列表。如何将视图中使用的模型返回给控制器以在 Post Action 中使用?Post 操作看起来像这样
[HttpPost]
public ActionResult AddFavourite(int userId, List<SelectableMovie> movies)
{
User user = db.Users.Single(u => u.ID == UserId);
//add items here
return View("Details", user);
}