2011-08-14 33 views
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 

回答

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 
+0

谢谢达林!现在它工作正常。我几乎花了一天的时间在此。再次感谢! – nora

相关问题