3

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

    public ViewResult AddFavourite(int id)
    {
        ViewBag.UserId = id;
        var movies = (from m in db.Movies
                             select new SelectableMovie() { Movie = m });
        return View(movies.ToList());
    }

and the view looks like

@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>
}

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

    [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);
    }

2 Answers 2

6
@for (int i = 0; i < Model.Count; i++)
{
    <tr>
        <td>
            @Html.CheckBox("movies[" + i + "].Selected", Model[i].Selected)
        </td>
        <td>
            @Html.Display("movies[" + i + "].Movie.Title", Model[i].Movie.Title)
        </td>
        <td>
            @Html.Display("movies[" + i + "].Movie.Description", Model[i].Movie.Description)
        </td>
        <td>
            @Html.Display("movies[" + i + "].Movie.ReleaseDate", Model[i].Movie.ReleaseDate)
        </td>
    </tr>
}

EDIT: this is good example: http://code-inside.de/blog-in/2012/09/17/modelbinding-with-complex-objects-in-asp-net-mvc/

7
  • using for didn't help. the list posted back is still an empty list. Oct 31, 2012 at 6:39
  • @RyanBurnham Try this then. The idea is that you should enumerate your movies.
    – karaxuna
    Oct 31, 2012 at 6:44
  • The issue wasn't with it not displaying in the view. but that link did help give me an idea. turns out it was because i was trying to set the movies parameter of the Post Method manually via an anonymous object. if i removed this property the mvc model binder worked. Oct 31, 2012 at 7:09
  • Sorry, I did not mention that you were passing movies from form route values :)
    – karaxuna
    Oct 31, 2012 at 7:14
  • @RyanBurnham but still you have to enumerate movie objects
    – karaxuna
    Oct 31, 2012 at 7:15
2

got it, the was with me trying to set the movies parameter manually in the anonymous object i create to include the user id. By including a movies property it was overriding the binding.

in my view the using statement

@using (Html.BeginForm("AddFavourite", "Users",
    new { userId = ViewBag.UserId, movies = Model }, FormMethod.Post))
{

should just be

 @using (Html.BeginForm("AddFavourite", "Users",
        new { userId = ViewBag.UserId }, FormMethod.Post))
    {

the MVC model binder handles the movie parameter in my Post Method

In addition to this as @karaxuna stated you need to use a for loop instead of a foreach loop otherwise it will not bind to the model.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.