Asp.Net MVC 2:视图模型在回发时到底如何绑定回模型?
抱歉,篇幅较长,但一张图片就值 1000 个字:
在 ASP.NET MVC 2 中,输入表单字段“名称”属性必须准确包含以下语法,您将使用该语法在 C# 中引用该对象,以便将其绑定回来发回后的对象。也就是说,如果您有一个如下所示的对象,其中包含多个具有多个 OrderLine 的 Orders,则名称看起来和工作起来都像这样(区分大小写):
这有效:
Order[0].id
Order[0].orderDate
Order[0].Customer.name
Order[0].Customer.Address
Order[0].OrderLine[0].itemID // first order line
Order[0].OrderLine[0].description
Order[0].OrderLine[0].qty
Order[0].OrderLine[0].price
Order[0].OrderLine[1].itemID // second order line, same names
Order[0].OrderLine[1].description
Order[0].OrderLine[1].qty
Order[0].OrderLine[1].price
但是我们想要在客户端浏览器中添加订单行和删除订单行。显然,索引必须从零开始,并包含到 N 的每个连续索引号。
黑带忍者 Phil Haack 的博客条目 此处解释了如何删除 [ 0] 索引,有重复的名称,并让 MVC 使用 [0] 表示法自动枚举重复的名称。但是,我未能使用嵌套对象将其绑定回来:
这失败了:
Order.id // Duplicate names should enumerate at 0 .. N
Order.orderDate
Order.Customer.name
Order.Customer.Address
Order.OrderLine.itemID // And likewise for nested properties?
Order.OrderLine.description
Order.OrderLine.qty
Order.OrderLine.price
Order.OrderLine.itemID
Order.OrderLine.description
Order.OrderLine.qty
Order.OrderLine.price
我还没有找到任何建议来描述它如何在帖子上绑定回嵌套 ViewModel。 是否有指向现有代码示例的链接或关于与 IList 进行嵌套绑定所需的确切名称的严格示例?
Steve Sanderson 具有执行此类操作的代码 此处,但我们似乎无法将此绑定回到嵌套对象。任何不具有 [0]..[n] 且编号连续的内容都会从返回对象中删除。
有什么想法吗?
Sorry for the length, but a picture is worth 1000 words:
In ASP.NET MVC 2, the input form field "name" attribute must contain exactly the syntax below that you would use to reference the object in C# in order to bind it back to the object upon post back. That said, if you have an object like the following where it contains multiple Orders having multiple OrderLines, the names would look and work well like this (case sensitive):
This works:
Order[0].id
Order[0].orderDate
Order[0].Customer.name
Order[0].Customer.Address
Order[0].OrderLine[0].itemID // first order line
Order[0].OrderLine[0].description
Order[0].OrderLine[0].qty
Order[0].OrderLine[0].price
Order[0].OrderLine[1].itemID // second order line, same names
Order[0].OrderLine[1].description
Order[0].OrderLine[1].qty
Order[0].OrderLine[1].price
However we want to add order lines and remove order lines at the client browser. Apparently, the indexes must start at zero and contain every consecutive index number to N.
The black belt ninja Phil Haack's blog entry here explains how to remove the [0] index, have duplicate names, and let MVC auto-enumerate duplicate names with the [0] notation. However, I have failed to get this to bind back using a nested object:
This fails:
Order.id // Duplicate names should enumerate at 0 .. N
Order.orderDate
Order.Customer.name
Order.Customer.Address
Order.OrderLine.itemID // And likewise for nested properties?
Order.OrderLine.description
Order.OrderLine.qty
Order.OrderLine.price
Order.OrderLine.itemID
Order.OrderLine.description
Order.OrderLine.qty
Order.OrderLine.price
I haven't found any advice out there yet that describes how this works for binding back nested ViewModels on post. Any links to existing code examples or strict examples on the exact names necessary to do nested binding with ILists?
Steve Sanderson has code that does this sort of thing here, but we cannot seem to get this to bind back to nested objects. Anything not having the [0]..[n] AND being consecutive in numbering simply drops off of the return object.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我们通过使用以下内容找到了解决方法:
其中 FieldPrefix 是“object[0]”。这不太理想,但效果确实不错。它简单而优雅。
We found a work around, by using the following:
Where FieldPrefix is the "object[0]". This is hardly ideal, but it certainly works pretty well. It's simple and elegant.