Vb.net MVC模型未绑定到HTTPPost操作
每当我从Vb.net MVC模型未绑定到HTTPPost操作,vb.net,post,razor,model-view-controller,controller,Vb.net,Post,Razor,Model View Controller,Controller,每当我从Respond.vbhtml页面提交表单时,控制器内的Respond\u post函数都会被调用;但是,传入FormsRespondeModel的项为空 为什么我的post操作没有像应该的那样填充我的FormsRespondeModel FormsRespondeModel.vb Public Class FormsRespondModel Public Property form As cihForm Public Property lists As cihLists
Respond.vbhtml
页面提交表单时,控制器内的Respond\u post
函数都会被调用;但是,传入FormsRespondeModel
的项为空
为什么我的post操作没有像应该的那样填充我的FormsRespondeModel
FormsRespondeModel.vb
Public Class FormsRespondModel
Public Property form As cihForm
Public Property lists As cihLists = New cihLists()
Public Property subOrgs As List(Of cihOrganizationSub)
Public Property Events As List(Of cihEvent)
Public Sub New()
End Sub
Public Sub New(formId As String)
form = New cihForm()
form.loadForm(Guid.Parse(formId))
lists.loadOrganizationSubs(ZenCommon.CurrentOrgId, ZenCommon.CurrentUserId)
Dim emptyList() As String = {}
Dim eventsearchList As cihEventSearch = New cihEventSearch(ZenCommon.CurrentOrgId, "", DateTime.Now, ZenCommon.Date2050, True, emptyList, emptyList, emptyList)
Events = eventsearchList.eventList
subOrgs = lists.organizationSubs
End Sub
End Class
<HttpPost>
<ActionName("Respond")>
Function Respond_post(viewModel As FormsRespondModel) As ActionResult
Return View("Respond", viewModel)
End Function
Respond.vbhtml
@ModelType CheckImHere.Student.FormsRespondModel
@Using Html.BeginForm()
@html.Hidden(Model.form.formId.ToString())
@For Each fld As Field In Model.form.fields
If fld.fieldTypeId = "text" Then
@<li>
<h4>@fld.title</h4>
<em>@fld.description</em>
<input type="text" placeholder="" value="@fld.response"/>
<em>@fld.isRequired</em>
</li>
End If
If fld.fieldTypeId = "para" Then
@<li>
<h4>@fld.title</h4>
<em>@fld.description</em>
@html.textarea(fld.response)
<em>@fld.isRequired</em>
</li>
End If
Next
<input type="submit" name="cmdSubmit" value="Submit" id="cmdSubmit"/>
End Using
@ModelType CheckImHere.Student.forms响应模型
@使用Html.BeginForm()
@html.Hidden(Model.form.formId.ToString())
@对于Model.form.fields中的每个fld As字段
如果fld.fieldTypeId=“text”,则
@
@外语职称
@飞行描述
@需要飞行训练
如果结束
如果fld.fieldTypeId=“para”,则
@
@外语职称
@飞行描述
@html.textarea(fld.response)
@需要飞行训练
如果结束
下一个
终端使用
RespondController.vb
Public Class FormsRespondModel
Public Property form As cihForm
Public Property lists As cihLists = New cihLists()
Public Property subOrgs As List(Of cihOrganizationSub)
Public Property Events As List(Of cihEvent)
Public Sub New()
End Sub
Public Sub New(formId As String)
form = New cihForm()
form.loadForm(Guid.Parse(formId))
lists.loadOrganizationSubs(ZenCommon.CurrentOrgId, ZenCommon.CurrentUserId)
Dim emptyList() As String = {}
Dim eventsearchList As cihEventSearch = New cihEventSearch(ZenCommon.CurrentOrgId, "", DateTime.Now, ZenCommon.Date2050, True, emptyList, emptyList, emptyList)
Events = eventsearchList.eventList
subOrgs = lists.organizationSubs
End Sub
End Class
<HttpPost>
<ActionName("Respond")>
Function Respond_post(viewModel As FormsRespondModel) As ActionResult
Return View("Respond", viewModel)
End Function
函数Respond_post(viewModel作为FormsRespondModel)作为ActionResult
返回视图(“响应”,视图模型)
端函数
为什么我的post操作没有像应该的那样填充我的FormsRespondeModel
因为您的
和
字段没有名称
属性。因此,当您提交表单时,绝对不会向服务器发送任何内容
您可以考虑使用强类型的帮助器,如“代码> HTML”、“文本Box”、“<代码”>“代码> HTML.TEXAXARAA/<代码>,以便生成适当的输入字段。您还可以查看以下博文,其中解释了如果希望模型绑定与集合一起使用,输入字段应如何准确命名:
因此,例如,要生成适当的索引器名称,可以将每个的替换为一个for
循环:
@For i As Integer = 0 To Model.form.fields.Count -1 Step 1
If Model.form.fields(i).fieldTypeId = "text" Then
@<li>
<h4>@Html.DisplayFor(Function(x)x.form.fields(i).title)</h4>
<em>@Html.DisplayFor(Function(x)x.form.fields(i).description)</em>
@Html.TextBoxFor(Function(x)x.form.fields(i).response)
<em>@Html.DisplayFor(Function(x)x.form.fields(i).isRequired)</em>
</li>
End If
If Model.form.fields(i).fieldTypeId = "para" Then
@<li>
<h4>@Html.DisplayFor(Function(x)x.form.fields(i).title)</h4>
<em>@@Html.DisplayFor(Function(x)x.form.fields(i).description)</em>
@Html.TextAreaFor(Function(x)x.form.fields(i).response)
<em>@Html.DisplayFor(Function(x)x.form.fields(i).isRequired)</em>
</li>
End If
Next
@For i As Integer=0 To Model.form.fields.Count-1步骤1
如果Model.form.fields(i).fieldTypeId=“text”则
@
@DisplayFor(函数(x)x.form.fields(i).title)
@DisplayFor(函数(x)x.form.fields(i).description)
@Html.TextBoxFor(函数(x)x.form.fields(i).response)
@DisplayFor(函数(x)x.form.fields(i).isRequired)
如果结束
如果Model.form.fields(i).fieldTypeId=“para”,则
@
@DisplayFor(函数(x)x.form.fields(i).title)
@@DisplayFor(函数(x)x.form.fields(i).description)
@Html.TextAreaFor(函数(x)x.form.fields(i).response)
@DisplayFor(函数(x)x.form.fields(i).isRequired)
如果结束
下一个
显然,在本例中,我们只有response
属性的输入字段。因此,这将是提交表单时发送到服务器的唯一内容,也是视图模型上将填充的唯一属性。如果要获取其他属性,可能需要将它们作为隐藏字段包含在表单中(使用@Html.HiddenFor
帮助程序)。也就是说,与其在表单中生成一堆隐藏字段,不如简单地包含一些唯一标识符作为隐藏字段,以便在POST操作中使用该标识符从后端检索这些属性