Note that there are some explanatory texts on larger screens.

plurals
  1. POPosting a List model back to the Controller
    primarykey
    data
    text
    <p>I'm stil new to ASP.net MVC3 and i'm trying to create a selectable list of items that can be added to a user. So the Get method looks like</p> <pre><code> public ViewResult AddFavourite(int id) { ViewBag.UserId = id; var movies = (from m in db.Movies select new SelectableMovie() { Movie = m }); return View(movies.ToList()); } </code></pre> <p>and the view looks like</p> <pre><code>@using MovieManager.Models @model List&lt;SelectableMovie&gt; @using (Html.BeginForm("AddFavourite", "Users", new { userId = ViewBag.UserId, movies = Model }, FormMethod.Post)) { &lt;table&gt; &lt;tr&gt; &lt;th&gt; Add &lt;/th&gt; &lt;th&gt; Title &lt;/th&gt; &lt;th&gt; Description &lt;/th&gt; &lt;th&gt; ReleaseDate &lt;/th&gt; &lt;/tr&gt; @foreach (var item in Model) { &lt;tr&gt; &lt;td&gt; @Html.CheckBoxFor(modelItem =&gt; item.Selected) &lt;/td&gt; &lt;td&gt; @Html.DisplayFor(modelItem =&gt; item.Movie.Title) &lt;/td&gt; &lt;td&gt; @Html.DisplayFor(modelItem =&gt; item.Movie.Description) &lt;/td&gt; &lt;td&gt; @Html.DisplayFor(modelItem =&gt; item.Movie.ReleaseDate) &lt;/td&gt; &lt;/tr&gt; } &lt;/table&gt; &lt;p&gt; &lt;input type="submit" value="Add Favourites" /&gt; &lt;/p&gt; } </code></pre> <p>This displays the list as intended but when i post back to the controller i get a userId but the movies parameter is an empty list. How do i get the model used in the view back to the controller to be used in the Post Action? The Post action looks like this</p> <pre><code> [HttpPost] public ActionResult AddFavourite(int userId, List&lt;SelectableMovie&gt; movies) { User user = db.Users.Single(u =&gt; u.ID == UserId); //add items here return View("Details", user); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload