Estoy usando RC1 de ASP.NET MVC.Carpeta modelo predeterminada y tipos complejos que incluyen una lista
Estoy tratando de ampliar Phil Haack's modelo de unión ejemplo. Estoy tratando de utilizar la carpeta de modelo por defecto para enlazar el objeto siguiente:
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; }
}
estoy usando el código del ejemplo de Phil con algunas alteraciones:
controlador:
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; }
}
}
Ver :
<%@ 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>
Mi problema es que los tipos simples (ID y el título) aparece en la Lista del producto obje ct, pero no la lista. Entonces:
- ¿Mi código es malo (no me sorprendería)?
- ¿Puede el encuadernador de modelo predeterminado manejar los objetos de ListOfProducts?
- Si el encuadernador de modelo predeterminado no manejará este tipo de objeto, ¿qué debo hacer (ejemplos si es posible)?
Gracias de antemano.
No eres un tonto. Estaba teniendo el mismo problema, pero fue porque mi lista no era una propiedad.Tenía algo análogo a: public List Artículos, en lugar de public List Items {get; conjunto; } –