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

5
我是使用 ASP.NET MVC 的 RC1 版本。
我正在尝试扩展 Phil Haack 的模型绑定示例。我正在尝试使用默认的模型绑定器来绑定以下对象:
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对象吗?
  • 如果默认模型绑定程序无法处理此类型的对象,我需要做什么(如果可能,请提供示例)?

提前致谢。

2个回答

6
回答我的问题:
我是个笨蛋!
我的示例无法工作,因为 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; } 

to:

public List<Product> Items { get; set; }

我的代码随后得以运作。

总之,默认模型绑定器可以处理包含List类型属性的类型。


3
你并不是一个傻瓜。我之前也遇到了同样的问题,原因是我的列表不是一个属性。我曾经定义了类似于public List<Product> Items这样的代码,而不是public List<Product> Items { get; set; }。 - Dave Markle

4

从RC 1开始:

  • 不再需要隐藏索引
  • []中的数字 必须以0开头,并且必须递增。

您的编号看起来没问题。

此外,我注意到您在项目属性名称上使用了不同的大小写。这不应该有任何影响,但值得检查。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接