asp.net – 默认模型绑定器和包含列表的复杂类型

我正在使用ASP.NET MVC的RC1.

我正在尝试扩展Phil Haack’s模型绑定示例.我正在尝试使用认模型绑定器来绑定以下对象:

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; }
}

我正在使用Phil的示例中的代码进行一些更改:

控制器:

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和Title)出现在productlist对象中,但不出现在List中.所以:

>我的代码是否不好(不会感到惊讶)?
>认模型绑定器可以处理listofProducts对象吗?
>如果认模型绑定器不能处理这种类型的对象,我需要做什么(如果可能的话,示例)?

提前致谢.

解决方法

回答我自己的问题:

我是假的!

我的示例不起作用,因为listofProducts类的Items属性不公开:

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; }

然后我的代码工作了.

总结认模型绑定器可以使用包含List类型属性的类型.

相关文章

### 创建一个gRPC服务项目(grpc服务端)和一个 webapi项目(...
一、SiganlR 使用的协议类型 1.websocket即时通讯协议 2.Ser...
.Net 6 WebApi 项目 在Linux系统上 打包成Docker镜像,发布为...
一、 PD简介PowerDesigner 是一个集所有现代建模技术于一身的...
一、存储过程 存储过程就像数据库中运行的方法(函数) 优点:...
一、Ueditor的下载 1、百度编辑器下载地址:http://ueditor....