默认的模式粘合剂和复杂的类型,包括一个列表
-
03-07-2019 - |
题
我使用的RC1的ASP.NET 视.
我试图延长 菲尔*哈克的 模型结合的例子。我想默认使用的模型的粘合剂,以结合以下目的:
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; }
}
我使用的代码,从菲尔的例子中有一些更改:
控制器:
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>
我的问题是简单的类型(身份证和标)显示在产品对象,但不清单。所以:
- 是我的码坏(不会感到惊讶)?
- 可以默认的模式粘合剂处理的ListOfProducts的对象?
- 如果缺省模式粘合剂不会处理这种类型的对象做些什么,我需要做(例如有可能)?
在此先感谢。
解决方案
回答我自己的问题:
我是个笨蛋!
我的例子不起作用,因为该项财产的ListOfProducts类是不公共:
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; }
和我的代码然后工作。
结束默认的模式粘合剂的工作的类型包含性质的类型清单。
其他提示
开始RC1:
- 隐藏的索引不再需要
- 在[] 必须 开始与和0 必须 提升。
你的号码看起来确定。
此外,我注意到你用不同套在你的品酒店的名字。这不应该使一个差异,但值得检查。
不隶属于 StackOverflow