views:

62

answers:

5

See below:

Edit.aspx View:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<Test.Models.Friend>" %>

Edit

<h2>Edit</h2>

<% using (Html.BeginForm()) {%>
    <%= Html.ValidationSummary(true) %>

    <fieldset>
        <legend>Fields</legend>

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

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

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

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

<% } %>

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

Controller for the edit:

[Authorize(Roles = "Administrator")]
    public ActionResult Edit(int id)
    {
        var eddy = friendsDB.Friends.Single(a => a.Id == id);
        return View(eddy);
    }
    [HttpPost]
    public ActionResult Edit(int id, string confirmButton)
    {
        var eddx = friendsDB.Friends.Single(a => a.Id == id);
        try
        {
            UpdateModel(eddx, "Friend");
            friendsDB.SubmitChanges();
            return RedirectToAction("Index");
        }
        catch
        {
            return View();
        }
    }

DOES NOT UPDATE so its not a matter of refreshing view. It goes through the motion, and returns to "Index" which is supposed to happen, but the record remains unedited. Any ideas or alternatives to editing with linq to sql ? Thanks.

A: 

try changing

var eddx = friendsDB.Friends.Single(a => a.Id == id);

to

var eddx = friendsDB.Friends.GetById(id);
GerManson
A: 

Or try changing

var eddx = friendsDB.Friends.Single(a => a.Id == id);

to

var eddx = (from q in friendsDB.Friends
    where q.Id == id
    select q).SingleOrDefault();

or (untested...)

var eddx = friendsDB.Friends.Where(a => a.Id == id).Single();
Pure.Krome
A: 

I suspect the issue isn't Linq to SQL at all but is related to your View Model, FormCollection and/or UpdateModel call.

Take a look at this article for example: http://www.joe-stevens.com/2010/02/17/asp-net-mvc-using-controller-updatemodel-when-using-a-viewmodel/

Single step through UpdateModel, did anything change?

Hightechrider
A: 

You need to tell the RedirectToAction("Index"); that you want to view the updated record. So maybe a RedirectToAction("Index", new {id = eddx.Id}); is what you are looking for.

Ahmad
th problem is its not editing. not that the view cant be updated.
Matiszac
A: 

I think the problem is that you do not bind the new object, so UpdateModel does not know from where to update your model.

So change the method signature to:

public ActionResult Edit(int id, FormCollection values, string confirmButton)

or

public ActionResult Edit(int id, Friend newfriend, string confirmButton)

In the second one you might need [Bind(Prefix = "Friend")] before Friend parameter if friend is not your viewmodel but only a part of your model.

apolka