Ryan Burnham
Ryan Burnham

Reputation: 2669

Posting a List model back to the Controller

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

Upvotes: 3

Views: 5994

Answers (2)

karaxuna
karaxuna

Reputation: 26930

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

Upvotes: 6

Ryan Burnham
Ryan Burnham

Reputation: 2669

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.

Upvotes: 2

Related Questions