默认模型绑定器和包含列表的复杂类型 [英] Default model binder and complex types that include a list

查看:21
本文介绍了默认模型绑定器和包含列表的复杂类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 ASP.NET MVC 的 RC1.

I'm using RC1 of ASP.NET MVC.

我正在尝试扩展 PhilHaack 的 模型绑定示例.我正在尝试使用默认模型绑定器来绑定以下对象:

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

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

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

My problem is that the simple types (Id and Title) appears in the productlist object, but not the List. So:

  • 我的代码是不是很糟糕(不会感到惊讶)?
  • 默认模型绑定器能否处理 ListOfProducts 对象?
  • 如果默认模型绑定器无法处理此类对象,我需要做什么(如果可能,请提供示例)?

提前致谢.

推荐答案

回答我自己的问题:

我是个傻瓜!

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

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

然后我的代码就可以工作了.

and my code then worked.

总结默认模型绑定器确实适用于包含 List 类型属性的类型.

To conclude the default model binder does work with types that contain properties of type List.

这篇关于默认模型绑定器和包含列表的复杂类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆