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.