Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Default model binder and complex types that include a list

I'm using RC1 of ASP.NET MVC.

I'm trying to extend Phil Haack's model binding example. I'm trying to use the default model binder to bind the following object:

public class ListOfProducts
{
    public int Id { get; set; }
    public string Title{ get; set; }
    List<Product> Items { get; set; }
}

public class Product
{
    public string Name { get; set; }
    public decimal Price { get; set; }
}

I'm using the code from Phil's example with some alterations:

Controller:

using System.Collections.Generic;
using System.Web.Mvc;

namespace TestBinding.Controllers
{
    [HandleError]
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            ViewData["Message"] = "Welcome to ASP.NET MVC!";

            return View();
        }

        //Action method on HomeController
        public ActionResult UpdateProducts(ListOfProducts productlist)
        {
            return View(productlist);
        }
    }

    public class Product
    {
        public string Name { get; set; }
        public decimal Price { get; set; }
    }

    public class ListOfProducts
    {
        public int Id { get; set; }
        public string Title { get; set; }
        List<Product> Items { get; set; }
    }
}

View:

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>

    <asp:Content ID="indexHead" ContentPlaceHolderID="head" runat="server">
        <title>Home Page</title>
    </asp:Content>

    <asp:Content ID="indexContent" ContentPlaceHolderID="MainContent" runat="server">
        <form method="post" action="/Home/UpdateProducts">
            <input type="text" name="productlist.id" value="99" />
            <input type="text" name="productlist.Title" value="SomeTitle" />

            <input type="hidden" name="productlist.Index" value="0" />
            <input type="text" name="productlist.items[0].Name" value="Beer" />
            <input type="text" name="productlist.items[0].Price" value="7.32" />

            <input type="hidden" name="productlist.Index" value="1" />
            <input type="text" name="productlist.Items[1].Name" value="Chips" />
            <input type="text" name="productlist.Items[1].Price" value="2.23" />

            <input type="hidden" name="productlist.Index" value="2" />
            <input type="text" name="productlist.Items[2].Name" value="Salsa" />
            <input type="text" name="productlist.Items[2].Price" value="1.23" />

            <input type="submit" />
        </form>
    </asp:Content>

My problem is that the simple types (Id and Title) appears in the productlist object, but not the List. So:

  • Is my code bad (wouldn't be surprised)?
  • Can the default model binder handle the ListOfProducts objects?
  • If the default model binder won't handle this type of object what do I need to do (examples if possible)?

Thanks in advance.

like image 486
Alan T Avatar asked Mar 05 '09 20:03

Alan T


2 Answers

To answer my own question:

I'm a dummy!

My example doesn't work because the Items property of the ListOfProducts class is not public:

public class ListOfProducts
{
    public int Id { get; set; }
    public string Title{ get; set; }
    List<Product> Items { get; set; }
}

I changed:

List<Product> Items { get; set; } 

to:

public List<Product> Items { get; set; }

and my code then worked.

To conclude the default model binder does work with types that contain properties of type List.

like image 191
Alan T Avatar answered Sep 30 '22 15:09

Alan T


Starting with RC 1:

  • Hidden Index is no longer required
  • The number in [] must start with 0 and must ascend.

Your numbering looks OK.

Also, I noticed that you used different casing on your items property name. That should not make a difference, but it's worth checking.

like image 24
Craig Stuntz Avatar answered Sep 30 '22 15:09

Craig Stuntz