0
我想批量更新。我想使用視圖模式。我讀this article 並創建程序。程序顯示數據庫值和html看起來不錯。但提交後,Controller無法獲取值。每個值都是'沒有',請指出我誤會的地方。由於ASP.NET MVC批量數據更新與視圖模型
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>
控制器 - 獲得
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
控制器 - 後
<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
謝謝達林!現在它工作正常。我幾乎花了一天的時間在此。再次感謝! – nora