MVC模型绑定编辑列表不工作 [英] MVC model binding to edit a list not working
问题描述
-
视图发出的HTML表单元素没有被索引(每个都被命名为数量和BoxID而不是[0] .Qty和[0] .BoxID)。我在这个子文件中看到的一切都说HTML.EditorFor和HiddenFor帮助者应该自动选择这个。
-
即使我手动更改视图来吐出
任何想法?我做错了吗?
这是以下视图:
@ ModelType IEnumerable(of HonorBox)
@Code
ViewData(Title)=索引
结束代码
< h2>索引< / h2>
@ Html.BeginForm(Index,HonorBoxes)
@ Html.AntiForgeryToken()
@For x = 0到模型.Count - 1
@< tr>
< td>
@ Html.DisplayFor(Function(i)Model(x).BoxID)
@ Html.HiddenFor(Function(i)Model(x).BoxID)
< / td>
< td>
@ Html.TextBoxFor(Function(i)Model(x).Qty)
@ Html.ValidationMessageFor(Function(i)Model(x).Qty)
< / td>
< / tr>
下一个
这些是控制器方法:
函数索引()作为ActionResult
Dim hb =从h在db.honorBoxes中选择h不在哪里h.Filled而不是h.Hold
返回视图(hb.ToList())
结束函数
< HttpPost>
函数索引(框为IEnumerable(Of HonorBox))作为ActionResult
如果ModelState.IsValid然后
对于每个框在框
Dim cbox = db.honorBoxes.Find(box。 BoxID)
如果不是IsDBNull(box.Qty)AndAlso cbox.Qty<> box.Qty然后
cbox.Qty = box.Qty
cbox.Filled = True
End If
Next
db.SaveChanges()
End If
返回RedirectToAction(索引)
结束函数
最后这里是模型
公共类HonorBox
< Key>公共属性BoxID As Integer
公共属性AssetID为Nullable(Of Integer)
公共属性资产作为资产
公共属性BoxType As String
公共属性保持为Nullable(Boolean)
公共属性填充为可空(Boolean)
公共属性数量为可空(整数)
结束类
为了模型绑定工具选择,模型类型需要为类型列表
not IEnumerable
。
将其更改为此将起作用:
@ModelType列表(HonorBox)
I can't figure out why this won't work. Everything I've read (both here and across the web) says this is how to bind to a list for editing but I'm not having any success. I have two problems:
The HTML form elements emitted by the view are not being indexed (each one is named "Qty" and "BoxID" instead of "[0].Qty" and "[0].BoxID"). Everything I've read on this subjuct says the HTML.EditorFor and HiddenFor helpers should pick this up automatically.
Even when I manually change the view to spit out the correct HTML (form elements with the right names) model binding isn't happening correctly and the collection parameter in the controller action method is null.
Any ideas? Am I doing something wrong?
Here's the view:
@ModelType IEnumerable(of HonorBox)
@Code
ViewData("Title") = "Index"
End Code
<h2>Index</h2>
@Html.BeginForm("Index", "HonorBoxes")
@Html.AntiForgeryToken()
@For x = 0 To Model.Count - 1
@<tr>
<td>
@Html.DisplayFor(Function(i) Model(x).BoxID)
@Html.HiddenFor(Function(i) Model(x).BoxID)
</td>
<td>
@Html.TextBoxFor(Function(i) Model(x).Qty)
@Html.ValidationMessageFor(Function(i) Model(x).Qty)
</td>
</tr>
Next
And these are the controller methods:
Function Index() As ActionResult
Dim hb = From h In db.honorBoxes Select h Where Not h.Filled And Not h.Hold
Return View(hb.ToList())
End Function
<HttpPost>
Function Index(boxes As IEnumerable(Of HonorBox)) As ActionResult
If ModelState.IsValid Then
For Each box In boxes
Dim cbox = db.honorBoxes.Find(box.BoxID)
If Not IsDBNull(box.Qty) AndAlso cbox.Qty <> box.Qty Then
cbox.Qty = box.Qty
cbox.Filled = True
End If
Next
db.SaveChanges()
End If
Return RedirectToAction("Index")
End Function
Finally here's the model
Public Class HonorBox
<Key> Public Property BoxID As Integer
Public Property AssetID As Nullable(Of Integer)
Public Property Asset As Asset
Public Property BoxType As String
Public Property Hold As Nullable(Of Boolean)
Public Property Filled As Nullable(Of Boolean)
Public Property Qty As Nullable(Of Integer)
End Class
In order for the model binder to pick it up the model type needs to be of type List
not IEnumerable
.
Changing it to this will work:
@ModelType List(of HonorBox)
这篇关于MVC模型绑定编辑列表不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!