将MVC上的绑定失败与复合模型绑定到回发上的简单模型

时间:2014-12-10 10:06:55

标签: asp.net vb.net asp.net-mvc-4 data-binding

我在MVC应用程序中有一个视图,它接受一个由其他类组合构成的模型,以显示我想要的所有数据。这很好但在回发后我想只返回我需要的值,以完成特定于该任务的更简单的类中的操作。

视图模型示例:

Public Class ManageEmployeeViewData
    Inherits BaseViewData


    Public Property Id As Integer
    Public Property CompanyId As Integer
    Public Property NewTaxCode As ManageTaxCodeUpdateModel
End Class

Public Class ManageTaxCodeUpdateModel

    Public EmployeeID As Integer
    Public TaxCode As String
    Public EffectiveDate As Date
End Class

视图示例:

<form action="@Url.Action("UpdateTaxCode")" method="post">
    @Html.HiddenFor(Function(f) f.NewTaxCode.EmployeeID)
    @Html.EditorRowFor(Function(f) f.NewTaxCode.TaxCode)
    @Html.EditorFor(Function(f) f.NewTaxCode.EffectiveDate)
    <button type="submit" class=" btn save"><span class="icon"></span>Save</button>
</form>

回发的控制器操作:

<HttpPost()>
Function UpdateTaxCode(<Bind(Prefix:="NewTaxCode")> ByVal model As ManageTaxCodeUpdateModel) As ActionResult
    Dim empSvc = ObjectFactory.GetInstance(Of IEmployeeService)()
    Dim employee = empSvc.GetEmployee(model.EmployeeID)
    Return (RedirectToAction("Edit", New With {.Id = model.EmployeeID}))
End Function

输出html的示例:

<form action="/inni/Company/2220/ManageEmployees/UpdateTaxCode" method="post" novalidate="novalidate">

    <input data-val="true" data-val-number="The field Int32 must be a number." data-val-required="The Int32 field is required." id="NewTaxCode_EmployeeID" name="NewTaxCode.EmployeeID" type="hidden" value="6339">

    <input class="text-box single-line valid" data-val="true" data-val-date="The field DateTime must be a date." data-val-required="The DateTime field is required." id="NewTaxCode_EffectiveDate" name="NewTaxCode.EffectiveDate" type="datetime" value="01/01/0001 00:00:00">

    <input class="text-box single-line" id="NewTaxCode_TaxCode" name="NewTaxCode.TaxCode" type="text" value=""><span class="field-validation-valid" data-valmsg-for="NewTaxCode.TaxCode" data-valmsg-replace="true">


    <button type="submit" class=" btn save"><span class="icon"></span>Save</button>

</form>

但是没有任何值绑定到ManageTaxCodeUpdateModel模型。我出错的任何想法?

0 个答案:

没有答案