【问题标题】:MVC Model not binding to HTTPPost actionMVC 模型未绑定到 HTTPPost 操作
【发布时间】:2015-02-28 23:52:14
【问题描述】:

每当我从Respond.vbhtml 页面提交我的表单时,我在控制器内的Respond_post 函数就会被正常调用;但是,FormsRespondModel 中传递的项目是空的。

为什么我的帖子操作没有按应有的方式填充我的 FormsRespondModel?

FormsRespondModel.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

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

RespondController.vb

<HttpPost>
<ActionName("Respond")>
Function Respond_post(viewModel As FormsRespondModel) As ActionResult

    Return View("Respond", viewModel)
End Function

【问题讨论】:

    标签: vb.net post razor model-view-controller controller


    【解决方案1】:

    为什么我的帖子操作没有按应有的方式填充我的 FormsRespondModel?

    因为您的 &lt;input&gt;&lt;textarea&gt; 字段没有 name 属性。因此,当您提交表单时,绝对不会向服务器发送任何内容。

    您可以考虑使用强类型帮助器,例如 Html.TextBoxForHtml.TeaxAreaFor,以便生成正确的输入字段。您还可以查看以下博客文章,其中解释了如果您希望模型绑定与集合一起使用,您的输入字段应该如何命名:http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx/

    例如,要生成正确的索引器名称,您可以将 For Each 替换为 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
    

    显然,在这个例子中,我们只有response 属性的输入字段。因此,当您提交表单时,这将是唯一发送到服务器的内容,并且是您的视图模型上将被填充的唯一属性。如果您想获取其他属性,您可能需要将它们作为隐藏字段包含在表单中(使用 @Html.HiddenFor 帮助器)。话虽如此,与其在表单中生成一堆隐藏字段,不如简单地包含一些唯一标识符作为隐藏字段,这将允许您使用此标识符在 POST 操作中从后端检索这些属性。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-20
      • 2014-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多