我正在尝试批量更新。我想使用viewmodel模式。我看过this article 并创建程序。程序显示数据库值和html看起来不错。但提交后,Controller无法获得价值。每个价值观都是'没什么',请指出我在哪里错过了解。感谢
Public Class users
Public id As String
Public username As String
End Class
视图模型
Public Class UserViewModel
Public userlist As List(Of users)
End Class
查看
<%@ Page Title="" Language="VB" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage(Of PartnerRotation.Repositories.UserViewModel)" %>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<% Html.BeginForm()%>
<input type="submit" name="input" value="Save" />
<table>
<tr><th>ID</th><th>Name</th></tr>
<% Dim i As Integer = 0 %>
<% For Each item In Model.userlist%>
<tr><td> <%: Model.userlist(i).id%>
<%: Html.HiddenFor(Function(Model) Model.userlist(i).id)%>
</td><td>
<%: Html.TextBoxFor(Function(Model) Model.userlist(i).username)%>
</td></tr>
<% i = i + 1%>
<% Next%>
</table>
<% Html.EndForm()%>
</asp:Content>
Controller -Get
Function Index() As ActionResult
Dim viewmodel As New UserViewModel With {
.userlist = (From u In _db.users Select New user With {
.id = u.id, _
.username = u.username}).ToList}
Return View(viewmodel)
End Function
Controller -Post
<HttpPost()> _
Function index(ByVal userlist As IList(Of user)) As ActionResult
For Each user In userlist
Dim id = user.id
Dim u = (From a In _db.PSC_MST _
Where a.id = id).FirstOrDefault
_db.ApplyCurrentValues(u.EntityKey.EntitySetName, user)
_db.SaveChanges()
Next
Return View()
End Function
答案 0 :(得分:1)
您需要使用properties
而不是模型类中的字段,以便默认模型绑定器可以设置它们:
Public Class users
Public Property id As String
Public Property username As String
End Class
Public Class UserViewModel
Public Property Userlist As List(Of users)
End Class