默认的模型绑定和复杂类型,包括列表 [英] Default model binder and complex types that include a list
问题描述
我使用ASP.NET MVC的RC1。
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; }
}
我使用的是code从菲尔的例子有一些变化:
I'm using the code from Phil's example with some alterations:
控制器:
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; }
}
}
查看:
<%@ 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>
我的问题是简单类型(Id和标题)出现在产品列表对象,但不在名单。所以:
My problem is that the simple types (Id and Title) appears in the productlist object, but not the List. So:
- 是我的code坏(不会感到惊讶)?
- 可以将默认模型绑定处理ListOfProducts对象?
- 如果默认的模型绑定不会处理这种类型的对象是什么,我需要做的(例子如果可能的话)?
先谢谢了。
推荐答案
要回答我的问题:
我是一个虚拟的!
我的例子不工作,因为ListOfProducts类的项目性质是不公开的:
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; }
}
我改了:
List<Product> Items { get; set; }
到
public List<Product> Items { get; set; }
和我的code然后工作了。
and my code then worked.
要总结默认模型绑定并与包含类型列表属性类型的工作。
To conclude the default model binder does work with types that contain properties of type List.
这篇关于默认的模型绑定和复杂类型,包括列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!