开发者

MVC2 Entity Framework - Update Model

First of all, am I the only developer on the planet that is attempting to use MVC with VB? I have searched tons of forums and read many posts and everyone that asks a question gives an example in C#. Anyway, now that I've got that out of the way, I've got a simple database table (User) with some columns (UserId, Username, FirstName, LastName). I am using the entity framework and on my edit view, I'm changing a value (Username) and clicking Save. In my Edit http post, I tell it to return to the Index and the value was not saved. Although, in my constructor for the http post, I return the object and it shows the new value...but that value doesn't make it to the db...any help?

My Controller:

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

My view:

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


Ok, so, aparently I AM the only VB developer using MVC. Anyway, I found the answer. It was in one of the beginner tutorials for MVC on the ASP.Net Site (Found Here). The answer was to change my Edit function (HttpPost) so that it utilizes the formvalues that were posted:

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜