开发者

ASP.NET MVC Bulk Data Update with viewModel

I'm trying to bulk update. I want to use viewmodel pattern. I read this article and create program. Program show Database value and html looks ok. But After submit, Controller couldn't get values. Every values are 'Nothing' please point me where I missunderstanding. Thanks

Public Class users

    Public id As String
    Public username As String

End Class

ViewModel

Public Class UserViewModel
    Public userlist As List(Of users)
End Class

View

<%@ 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">

&l开发者_StackOverflow中文版t;%  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


You need to use properties instead of fields in your model classes, so that the default model binder could set them:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜