【问题标题】:MVC2 Entity Framework - Update ModelMVC2 实体框架 - 更新模型
【发布时间】:2010-11-16 03:01:37
【问题描述】:

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

我的控制器:

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>

【问题讨论】:

    标签: asp.net-mvc-2 viewmodel


    【解决方案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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-26
      • 2013-07-15
      相关资源
      最近更新 更多