2015-01-02 283 views
1

每當我從Respond.vbhtml頁面提交我的表單時,控制器中的我的Respond_post函數就會被調用;但是,在FormsRespondModel中傳遞的項目是空的。MVC Model not binding to HTTPPost action

enter image description here

爲什麼我的帖子動作不填充我的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 

回答

3

爲什麼我的發佈操作沒有按照它應該填充我的FormsRespondModel?

因爲你<input><textarea>領域沒有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操作中從後端檢索這些屬性。