gpt4 book ai didi

asp.net - 默认模型绑定(bind)器和包含列表的复杂类型

转载 作者:行者123 更新时间:2023-12-04 17:29:15 25 4
gpt4 key购买 nike

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

我正在尝试扩展 Phil Haack's模型绑定(bind)示例。我正在尝试使用默认模型绑定(bind)器来绑定(bind)以下对象:

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 示例中的代码进行一些更改:

Controller :
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。所以:
  • 我的代码不好(不会感到惊讶)?
  • 默认模型绑定(bind)器可以处理 ListOfProducts 对象吗?
  • 如果默认模型绑定(bind)器不能处理这种类型的对象,我需要做什么(如果可能的话)?

  • 提前致谢。

    最佳答案

    回答我自己的问题:

    我是个假人!

    我的示例不起作用,因为 ListOfProducts 类的 Items 属性不是公共(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; }

    然后我的代码就起作用了。

    总而言之,默认模型绑定(bind)器确实适用于包含 List 类型属性的类型。

    关于asp.net - 默认模型绑定(bind)器和包含列表的复杂类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/616559/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com