2

上下文:ASP MVC 3/4,VB.net

我正在尝试绑定到具有复杂类型列表属性的对象。
问题陈述:在回发时,我在除复杂类型属性之外的所有属性中获取值,它被返回为空。我做错了什么,因为默认模型绑定器无法绑定我的属性,这是一个复杂类型的列表。

我究竟做错了什么?


我的视图模型类:

Public Class Customer  
    Public Property ID As Integer  
    Public Property FirstName As String  
    Public Property LastName As String          
    Public Appointments As IList(Of Appointment)  
End Class  

Public Class Appointment
    Public Property ClientName As String
    Public Property [Date] As DateTime
    Public Property TermsAccepted As Boolean
End Class

我在控制器中的操作方法如下所示

    Function CreateCusotmerWithMultipleBookings() As ActionResult
        Dim Modeldata As New Customer With {.ID = 1, .FirstName = "First Name", .LastName = "Last Name"}
        Modeldata.Appointments = New List(Of Appointment) From {New Appointment, New Appointment, New Appointment}
        Return View(Modeldata)
    End Function

    <HttpPost()> _
    Function CreateCusotmerWithMultipleBookings(FormData As Customer) As ActionResult
        Return View(FormData)
    End Function

我的观点是这样的:

@ModelType Customer
@Using Html.BeginForm  
@Html.EditorForModel 
If Model.Appointments IsNot Nothing AndAlso Model.Appointments.Count > 0 Then
    For i As Integer = 0 To Model.Appointments.Count - 1
        @:<b >Appointment @i</b><br />
        @Html.TextBoxFor(Function(x) x.Appointments(i).ClientName) @:<br />
        @Html.TextBoxFor(Function(x) x.Appointments(i).Date) @:<br />
        @Html.TextBoxFor(Function(x) x.Appointments(i).TermsAccepted)@:<br />            
    Next
End If
End Using

从这个视图生成的 HTML 类似于:(我删除了不必要的标记以方便阅读)

<body>
<form action="/testarea/Appointment/CreateCusotmerWithMultipleBookings" method="post">    
    <input id="ID" name="ID" type="number" value="1" />
    <input class="text-box single-line" id="FirstName" name="FirstName" type="text" value="First Name" />
    <input class="text-box single-line" id="LastName" name="LastName" type="text" value="Last Name" />
    <input id="Appointments_0__ClientName" name="Appointments[0].ClientName" type="text" value="" />
    <input id="Appointments_0__Date" name="Appointments[0].Date" type="text" value="1/1/0001 12:00:00 AM" />
    <input id="Appointments_0__TermsAccepted" name="Appointments[0].TermsAccepted" type="text" value="False" />
    <input id="Appointments_1__ClientName" name="Appointments[1].ClientName" type="text" value="" />
    <input id="Appointments_1__Date" name="Appointments[1].Date" type="text" value="1/1/0001 12:00:00 AM" />
    <input id="Appointments_1__TermsAccepted" name="Appointments[1].TermsAccepted" type="text" value="False" />
    <input id="Appointments_2__ClientName" name="Appointments[2].ClientName" type="text" value="" />
    <input id="Appointments_2__Date" name="Appointments[2].Date" type="text" value="1/1/0001 12:00:00 AM" />
    <input id="Appointments_2__TermsAccepted" name="Appointments[2].TermsAccepted" type="text" value="False" />
    <input type="submit" value="Create customer with multiple appointments" />
</form>

再次提问:
为什么下面控制器中的 formdata.Appointments 没有任何价值?

        <HttpPost()> _
    Function CreateCusotmerWithMultipleBookings(FormData As Customer) As ActionResult
        Return View(FormData)
    End Function
4

0 回答 0