asp.net – 默认模型绑定器和包含列表的复杂类型
发布时间:2020-12-16 03:31:54 所属栏目:asp.Net 来源:网络整理
导读:我正在使用ASP.NET MVC的RC1. 我正在尝试扩展Phil Haack’s模型绑定示例.我正在尝试使用默认模型绑定器来绑定以下对象: public class ListOfProducts{ public int Id { get; set; } public string Title{ get; set; } ListProduct Items { get; set; }}publ
我正在使用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类的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类型属性的类型. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net – 是否有可能过滤SignalR中的接收器?
- asp.net – 我可以在DefaultModelBinder上禁用DataAnnotati
- asp.net – 无Cookie属性web.config
- 调试w /附加到进程启动ASP.NET开发服务器
- asp.net-mvc – Razor视图与部分视图
- asp.net-mvc – 更改ASP.NET MVC筛选器中的视图
- ASP.NET清空缓存时遇到的问题简析
- asp.net – 错误:必须声明标量变量“@data”
- asp.net-core – ASP.Net 5类库中的EntityFramework命令?
- asp.net-mvc – ASP.NET MVC如何防止重新发布
推荐文章
站长推荐
- asp.net – 获取客户端隐藏字段的值
- asp.net-mvc – ASP.NET MVC – Cascading Drop
- asp.net-mvc-4 – ASP.Net MVC JS / CSS捆绑在部
- asp.net-mvc – 为什么ASP.NET MVC 4与IList for
- asp-classic – 如何从Classic ASP Request获取*
- asp.net – 流程模型minFreeThreads澄清
- asp.net – 如何使用MVC属性验证列表是否具有大于
- 子文件夹中忽略ASP.NET web.config授权设置
- ASP.NET通过自定义函数实现对字符串的大小写切换
- asp.net – Gridview在列标题中排序/向下箭头
热点阅读