似乎我的问题类似于MVC model binding to edit a list not working但是我遇到的问题是返回的列表是Nothing。还试图遵循http://www.c-sharpcorner.com/uploadfile/4b0136/editing-multiple-records-using-model-binding-in-mvc/但转换为VB.NET
Function EditAll() As ActionResult
Return View(db.Recipients.Where(Function(r) r.Type = "EMAIL").ToList())
End Function
<HttpPost()>
Public Function EditAll(recipients As List(Of Recipient)) As ActionResult
'recipients is Nothing why???
For Each item As Recipient In recipients
Dim Existing_Recip As Recipient = db.Recipients.Find(item.ID)
Existing_Recip.EmailAddress = item.EmailAddress
Existing_Recip.Active = item.Active
Next
db.SaveChanges()
Return View(recipients)
End Function
观点:
@ModelType List(Of ConcurBridge.Models.Concur.Recipient)
@Code
ViewData("Title") = "EditAll"
Layout = "~/Views/Shared/_Layout.vbhtml"
End Code
<h2>Edit All Recipients</h2>
@Using (Html.BeginForm("EditAll", "Recipients", FormMethod.Post))
@<table class="table">
@For i As Integer = 0 To Model.Count - 1
@<tr>
<td>
@Html.Hidden("recipient.Index", i)
@Html.TextBox("recipient[" + i.ToString + "].ID",
Model(i).ID, New With {.readonly = "readonly"})
</td>
<td>
@Html.TextBox("recipient[" + i.ToString + "].EmailAddress",
Model(i).EmailAddress)
</td>
<td>
@Html.CheckBox("recipient[" + i.ToString + "].Active",
Model(i).Active)
</td>
</tr>
Next
<tr>
<td colspan="3">
<input type="submit" value="Submit" />
</td>
</tr>
</table>
End Using
型号:
<Table("MultiUseTable")>
Public Class Recipient
Public Property ID As Integer
Public Property Type As String
<DataType(DataType.EmailAddress),
Column("Value"),
DisplayName("Email Address"),
Required(AllowEmptyStrings:=False, ErrorMessage:="Email address is required")>
Public Property EmailAddress As String
Public Property Active As Boolean
End Class