Eric Herlitz
Eric Herlitz

Reputation: 26267

Update View (from sql server) in asp.net mvc3

I display data from a (sql)view with information from several tables which I created in MS SQL Server. The collected data is shown in a strongly typed view in an asp.net mvc3 site using entity framework.

When creating the edit page everything shows up as expected, it's when i'm trying to save my modified details stuff wont work.

The page builds

Some code

Method to display the edit page

public ActionResult Edit(Guid id)
{
    return View(db.ViewIndividualAlls.Where(x => x.UserGuid == id).SingleOrDefault());
}

Method to receive the HttpPost

[HttpPost]
public ActionResult Edit(Guid id, ViewIndividualAll viewIndividualAll)
{
    // Ind confirmed in the debugger, it works!
    var ind = db.ViewIndividualAlls.Where(x => x.UserGuid == id).SingleOrDefault();
    //var ind = db.ViewIndividualAlls.Find(viewIndividualAll);
    try
    {
        if (TryUpdateModel(ind))
        {
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        else
        {
            ViewData["error"] = "Model validation failed!";
            return View(viewIndividualAll);
        } 
    }
    catch (Exception e)
    {
        ViewData["error"] = string.Concat(e.Message, " ", e.StackTrace);
        return View(viewIndividualAll);
    }
}

Error message

The property 'whatever' is part of the object's key information and cannot be modified.

Kind of strange since I'm able to update pretty much anything in sql server management studio, except stuff that's bound with foreign keys of course.

Any advices (related) at all would be appreciated

Thanks!

Update

I checked the primary keys in the model browser and noticed they where all wrongly set so I fixed it. Now when I fixed that I get this new error

Unable to update the EntitySet 'ViewIndividualAll' because it has a DefiningQuery 
and no <UpdateFunction> element exists in the <ModificationFunctionMapping> 
element to support the current operation.

So no joy, more fixing, but still interested in help!

Upvotes: 0

Views: 1243

Answers (2)

Eric Herlitz
Eric Herlitz

Reputation: 26267

Solved

[HttpPost]
public ActionResult Edit(Guid id, ViewIndividualAll viewIndividualAll)
{
    var ind = db.ViewIndividualAlls.Find(id);
    try
    {
        if (TryUpdateModel(ind))
        {
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        else
        {
            ViewData["error"] = "Model validation failed!";
            return View(viewIndividualAll);
        } 
    }
    catch (Exception e)
    {
        ViewData["error"] = string.Concat(e.Message, " ", e.StackTrace, "\n", e.InnerException, "\n", e.HelpLink);
        return View(viewIndividualAll);
    }
}

Upvotes: 0

juhan_h
juhan_h

Reputation: 4011

From the looks of it you are working with Guid as a key. Entity Framework gets messy with Guids as keys. The error you are getting is probably because TryUpdateModel(ind) touches the Guid value. You should either mark the Guid key with [ScaffoldColumn(false)] (not sure whether this would help) or manually copy submitted fields to your entity from database.

Upvotes: 1

Related Questions