MVC2实体框架 - 更新模型

时间:2010-11-16 03:01:38

标签: asp.net-mvc-2 viewmodel

首先,我是地球上唯一一个试图在VB中使用MVC的开发人员吗?我已经搜索了大量的论坛并阅读了很多帖子,每个人都问一个问题在C#中给出了一个例子。无论如何,现在我已经完成了这个,我有一个简单的数据库表(User),其中包含一些列(UserId,Username,FirstName,LastName)。我正在使用实体框架,在我的编辑视图中,我正在更改值(用户名)并单击“保存”。在我的编辑http帖子中,我告诉它返回索引并且未保存该值。虽然,在我的http帖子的构造函数中,我返回对象,它显示了新的值......但是这个值没有进入db ...任何帮助?

我的控制器:

Function Edit(ByVal ID As Guid) As ActionResult
        'get the user
        Dim usr = (From u In db.Users
                  Where u.UserId = ID
                  Select u).Single

        Return View(usr)
    End Function

    <HttpPost()> _
    Function Edit(ByVal ID As Guid, ByVal usrInfo As User, ByVal formValues As FormCollection) As ActionResult
        '            Dim usr As User = db.Users.Single(Function(u) u.UserId = ID)

        If ModelState.IsValid Then
            TryUpdateModel(usrInfo, "User")

            Return RedirectToAction("Index")
        Else
            Return View(usrInfo)
        End If
    End Function

我的观点:

    <h2>Edit</h2>

<%-- The following line works around an ASP.NET compiler warning --%>
<%: ""%>

<% Using Html.BeginForm() %>
    <%: Html.ValidationSummary(True) %>
    <fieldset>
        <legend>Fields</legend>

        <div class="editor-label">
            <%: Html.LabelFor(Function(model) model.UserId) %>
        </div>
        <div class="editor-field">
            <%: Html.TextBoxFor(Function(model) model.UserId) %>
            <%: Html.ValidationMessageFor(Function(model) model.UserId) %>
        </div>

        <div class="editor-label">
            <%: Html.LabelFor(Function(model) model.Username) %>
        </div>
        <div class="editor-field">
            <%: Html.TextBoxFor(Function(model) model.Username) %>
            <%: Html.ValidationMessageFor(Function(model) model.Username) %>
        </div>

        <div class="editor-label">
            <%: Html.LabelFor(Function(model) model.FirstName) %>
        </div>
        <div class="editor-field">
            <%: Html.TextBoxFor(Function(model) model.FirstName) %>
            <%: Html.ValidationMessageFor(Function(model) model.FirstName) %>
        </div>

        <div class="editor-label">
            <%: Html.LabelFor(Function(model) model.LastName) %>
        </div>
        <div class="editor-field">
            <%: Html.TextBoxFor(Function(model) model.LastName) %>
            <%: Html.ValidationMessageFor(Function(model) model.LastName) %>
        </div>

        <div class="editor-label">
            <%: Html.LabelFor(Function(model) model.CreatedDate) %>
        </div>
        <div class="editor-field">
            <%: Html.TextBoxFor(Function(model) model.CreatedDate, String.Format("{0:g}", Model.CreatedDate)) %>
            <%: Html.ValidationMessageFor(Function(model) model.CreatedDate) %>
        </div>

        <div class="editor-label">
            <%: Html.LabelFor(Function(model) model.CreatedBy) %>
        </div>
        <div class="editor-field">
            <%: Html.TextBoxFor(Function(model) model.CreatedBy) %>
            <%: Html.ValidationMessageFor(Function(model) model.CreatedBy) %>
        </div>

        <div class="editor-label">
            <%: Html.LabelFor(Function(model) model.LastLogin) %>
        </div>
        <div class="editor-field">
            <%: Html.TextBoxFor(Function(model) model.LastLogin, String.Format("{0:g}", Model.LastLogin)) %>
            <%: Html.ValidationMessageFor(Function(model) model.LastLogin) %>
        </div>

        <p>
            <input type="submit" value="Save" />
        </p>
    </fieldset>

<% End Using %>

<div>
    <%: Html.ActionLink("Back to List", "Index") %>
</div>

1 个答案:

答案 0 :(得分:1)

好的,所以,显然我是使用MVC的唯一VB开发人员。无论如何,我找到了答案。它是ASP.Net站点(Found Here)上MVC的初学者教程之一。答案是更改我的编辑功能(HttpPost),以便它使用已发布的表单值:

<HttpPost()> _
    Function Edit(ByVal id As Guid, ByVal collection As FormCollection) As ActionResult
        Dim rest = (From r In db.Restaurants
                Where r.RestaurantId = id
                Select r).Single()

        Try

            TryUpdateModel(rest, collection.ToValueProvider())

            db.SaveChanges()

            Return RedirectToAction("Index")
        Catch
            Return View(rest)
        End Try
    End Function