Note that there are some explanatory texts on larger screens.

plurals
  1. PODefault model binder and complex types that include a list
    primarykey
    data
    text
    <p>I'm using RC1 of ASP.NET MVC.</p> <p>I'm trying to extend <a href="http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx" rel="nofollow noreferrer">Phil Haack's</a> model binding example. I'm trying to use the default model binder to bind the following object:</p> <pre><code>public class ListOfProducts { public int Id { get; set; } public string Title{ get; set; } List&lt;Product&gt; Items { get; set; } } public class Product { public string Name { get; set; } public decimal Price { get; set; } } </code></pre> <p>I'm using the code from Phil's example with some alterations:</p> <p>Controller:</p> <pre><code>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&lt;Product&gt; Items { get; set; } } } </code></pre> <p>View:</p> <pre><code>&lt;%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %&gt; &lt;asp:Content ID="indexHead" ContentPlaceHolderID="head" runat="server"&gt; &lt;title&gt;Home Page&lt;/title&gt; &lt;/asp:Content&gt; &lt;asp:Content ID="indexContent" ContentPlaceHolderID="MainContent" runat="server"&gt; &lt;form method="post" action="/Home/UpdateProducts"&gt; &lt;input type="text" name="productlist.id" value="99" /&gt; &lt;input type="text" name="productlist.Title" value="SomeTitle" /&gt; &lt;input type="hidden" name="productlist.Index" value="0" /&gt; &lt;input type="text" name="productlist.items[0].Name" value="Beer" /&gt; &lt;input type="text" name="productlist.items[0].Price" value="7.32" /&gt; &lt;input type="hidden" name="productlist.Index" value="1" /&gt; &lt;input type="text" name="productlist.Items[1].Name" value="Chips" /&gt; &lt;input type="text" name="productlist.Items[1].Price" value="2.23" /&gt; &lt;input type="hidden" name="productlist.Index" value="2" /&gt; &lt;input type="text" name="productlist.Items[2].Name" value="Salsa" /&gt; &lt;input type="text" name="productlist.Items[2].Price" value="1.23" /&gt; &lt;input type="submit" /&gt; &lt;/form&gt; &lt;/asp:Content&gt; </code></pre> <p>My problem is that the simple types (Id and Title) appears in the productlist object, but not the List. So:</p> <ul> <li>Is my code bad (wouldn't be surprised)?</li> <li>Can the default model binder handle the ListOfProducts objects?</li> <li>If the default model binder won't handle this type of object what do I need to do (examples if possible)?</li> </ul> <p>Thanks in advance.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload