模型绑定到列表
如何制作默认模型绑定句柄列表?
假设我有一个 ShoppingCart 类,其中有一个 ShoppingCartItem 列表:
Public Class ShoppingCart
Public Property CouponCode As String
Public Property Items As New List(Of ShoppingCartItem)
End Class
Public Class ShoppingCartItem
Public Property Title As String
Public Property Count As Integer
End Class
@ModelType VAVTag.LuckyDraw.ShoppingCart
@Code
Layout = Nothing
End Code
<!DOCTYPE html>
<html>
<head runat="server">
<title>Index</title>
</head>
<body>
@Using Html.BeginForm()
@<fieldset>
<legend>ShoppingCart</legend>
@Html.TextBoxFor(Function(m) m.CouponCode)
<hr />
@Html.EditorFor(Function(m) m.Items)
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
End Using
</body>
</html>
这些项目是使用 EditorTemplate 渲染(感谢 Darin):
@ModelType ShoppingCartItem
@Html.EditorFor(Function(m) m.Title )
@Html.EditorFor(Function(m) m.Count )
<br />
在我的控制器中,我插入一些随机数据,该视图很好地呈现了我的购物车,其中包含三件商品。
Public Class ShoppingCartController
Inherits System.Web.Mvc.Controller
Function Index() As ActionResult
Dim model As New ShoppingCart
model.Items.Add(New ShoppingCartItem With {.Count = 1, .Title = "Item A"})
model.Items.Add(New ShoppingCartItem With {.Count = 17, .Title = "Item B"})
model.Items.Add(New ShoppingCartItem With {.Count = 100, .Title = "Item C"})
Return View(model)
End Function
<HttpPost()>
Function Index(model As ShoppingCart) As ActionResult
Return View(model) ' model is empty!
End Function
End Class
但是,当我提交页面时,没有拾取任何值,甚至没有拾取优惠券代码字段。模型对象为空。什么给?
最终,我的目标是通过 JavaScript 在客户端添加/删除项目,然后让 modelbinder 在提交页面时自动获取更改。
更新:我只是在模型属性声明中缺少 Property
关键字。已经太晚了,我得睡一会儿了。 :)
How do I make the default modelbinding handle Lists?
Let’s say I have a ShoppingCart class, which has a ShoppingCartItem List:
Public Class ShoppingCart
Public Property CouponCode As String
Public Property Items As New List(Of ShoppingCartItem)
End Class
Public Class ShoppingCartItem
Public Property Title As String
Public Property Count As Integer
End Class
My view looks like this (as per the advice from this, and this, blogpost):
@ModelType VAVTag.LuckyDraw.ShoppingCart
@Code
Layout = Nothing
End Code
<!DOCTYPE html>
<html>
<head runat="server">
<title>Index</title>
</head>
<body>
@Using Html.BeginForm()
@<fieldset>
<legend>ShoppingCart</legend>
@Html.TextBoxFor(Function(m) m.CouponCode)
<hr />
@Html.EditorFor(Function(m) m.Items)
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
End Using
</body>
</html>
The items are rendered using an EditorTemplate (thanks Darin):
@ModelType ShoppingCartItem
@Html.EditorFor(Function(m) m.Title )
@Html.EditorFor(Function(m) m.Count )
<br />
In my controller, I’m inserting some random data, and the view renders my shoppingcart nicely, with three items.
Public Class ShoppingCartController
Inherits System.Web.Mvc.Controller
Function Index() As ActionResult
Dim model As New ShoppingCart
model.Items.Add(New ShoppingCartItem With {.Count = 1, .Title = "Item A"})
model.Items.Add(New ShoppingCartItem With {.Count = 17, .Title = "Item B"})
model.Items.Add(New ShoppingCartItem With {.Count = 100, .Title = "Item C"})
Return View(model)
End Function
<HttpPost()>
Function Index(model As ShoppingCart) As ActionResult
Return View(model) ' model is empty!
End Function
End Class
However, when I submit the page, no values are picked up, not even the CouponCode field. The model object is empty. What gives?
Ultimately, my goal is to add/remove items clientside via javascript and then have the modelbinder pick up the changes automatically when the page is submitted.
Update: I was just missing the Property
keyword from my model properties declarations. It's too late, and I got to get some sleep. :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
默认模型绑定器已经可以很好地处理列表和字典。
只需使用编辑器模板即可。例如:
视图模型:
控制器:
视图:
最后是编辑器模板(
~/Views/Home/EditorTemplates/ShoppingCartItem.cshtml
),它将为Items
的每个项目呈现代码>集合:The default model binder already handles lists and dictionaries perfectly fine.
Just use editor templates. For example:
View model:
Controller:
View:
and finally the editor template (
~/Views/Home/EditorTemplates/ShoppingCartItem.cshtml
) which will be rendered for each item of theItems
collection: