views:

1505

answers:

2

Hi All

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.

+4  A: 

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.

Craig Stuntz
Hi Craig, Thanks for the tips, much appreciated.
Alan T
+1 saved my day
alexandrul
+2  A: 

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.

Alan T