关于控制器操作上的模型绑定,我有两个几乎相同的场景。一种有效,一种无效。我不知道为什么。
这有效:
鉴于此 ViewModel 类:
Public Class SeasonCreateViewModel
Public Property Season As Season
End Class
我们有这些动作
Function Create() As ActionResult
Dim seasonVM As New SeasonCreateViewModel()
Return View("Create", seasonVM)
End Function
<HttpPost()>
<ValidateAntiForgeryToken()>
Function Create(seasonVM As SeasonCreateViewModel) As ActionResult
End Function
一切都完美结合。 seasonVM.Season
包含从表单发布的值。
但是,这不起作用:
鉴于此 ViewModel 类:
Public Class UserCreateViewModel
Public UserPerson As UserPersonModel
End Class
而这些动作:
Function Create() As ActionResult
Dim userVM As New UserCreateViewModel()
Return View("Create", userVM)
End Function
'
' POST: /Admin/User/Create
<HttpPost()>
<ValidateAntiForgeryToken()>
Function Create(userVM As UserCreateViewModel) As ActionResult
End Function
userVM.UserPerson
不以相同的方式绑定到表单值seasonVM.Season
。事实上,它是Nothing
(又名。null
)
有没有人有任何想法?
如果您对视图感到好奇,它们的结构是相同的,如下所示:
@Using Html.BeginForm()
@Html.AntiForgeryToken()
@Html.ValidationSummary(True)
<div class="editor-label">
@Html.LabelFor(Function(model) model.UserPerson.NewUsername)
</div>
<div class="editor-field">
@Html.EditorFor(Function(model) model.UserPerson.NewUsername)
@Html.ValidationMessageFor(Function(model) model.UserPerson.NewUsername)
</div>
<p>
<input type="submit" value="Create" />
</p>
End Using
和
@Using Html.BeginForm()
@Html.AntiForgeryToken()
@Html.ValidationSummary(True)
<div class="editor-label">
@Html.LabelFor(Function(model) model.Season.SeasonDescription)
</div>
<div class="editor-field">
@Html.EditorFor(Function(model) model.Season.SeasonDescription)
@Html.ValidationMessageFor(Function(model) model.Season.SeasonDescription)
</div>
<p>
<input type="submit" value="Create" />
</p>
End Using
请注意:我省略了不相关的代码,主要是视图页面上的附加属性。我会说我没有名为“userVM”的属性,UserPersonModel
就像这里的情况一样:提交表单时模型为空
更新
好的。我想我已经准备好放弃弄清楚为什么 Season 可以正确绑定,但 UserPerson 不是。
我以为我已经找到了答案,但实际上似乎并没有什么不同:
我有
Public Class SeasonCreateViewModel
Public Property Season As Season
End Class
我有
Public Class UserCreateViewModel
Public UserPerson As UserPersonModel
End Class
当这样排列时,差异似乎很明显。在SeasonCreateViewModel
中,我有一个Season
与类同名的属性,它是 ( Season
) 的实例。在其中UserCreateViewModel
,我有一个属性UserPerson
,它的名称与它的类稍有不同UserPersonModel
。因此,我认为模型绑定器不会自动匹配userVM.UserPerson
到其对应的类。
因此,我将类更改为UserPersonModel
,UserPerson
以便表单值以与它们相同的方式匹配Season
(即与类名匹配),但它仍然没有修复它。
然而,解决它的方法是,如果我改变它:
Function Create(userVM As UserCreateViewModel) As ActionResult
对此
Function Create(userPerson As UserPerson) As ActionResult
为什么这突然正确绑定,以前没有?我不知道。不过,这是否有助于任何人回答这个问题?