views:

28

answers:

1

I am just now starting to work with LINQ, and am pretty familiar with MVC. I have a strongly typed view that is updating a record. I have successfully done a creation:

This works fine, and creates a record in the database:

public ActionResult Create(TABLEMODEL tableModel)
{
    DBDataContext db = new DBDataContext();
    if (ModelState.IsValid)
    {
        db.TABLEMODEL.InsertOnSubmit(tableModel);
        db.SubmitChanges();
    }
}

But when trying to update:

public ActionResult Manage(TABLEMODEL tableModel)
{
    DBDataContext db = new DBDataContext();
    if (ModelState.IsValid)
    {
        db.SubmitChanges();
    }
}

This fails, in the sense that it does not update the record in the database. No actual error/exception occurs, and I can step through it just fine.

I am sure I am missing something, but cannot find what. I appreciate any help on this matter.

UPDATE
I did notice that if I get a record using the DataContext:

DBDataContext db = new DBDataContext();
var m = db.TABLEMODELs.Single(m => m.ID == 1);
m.Name = "UpdatedName";
db.SubmitChanges();

This does update, so I assume I am somehow not binding from my model to the LINQ context.

My Solution
I found that you need to retrieve the object and then update that with the form. Simple enough.

[HttpPost]
public ActionResult Manage(int ID, FormCollection form)
{
    DBSDataContext db = new DBSDataContext();
    var t= db.TABLEMODELs.Single(b => b.ID == ID);
    UpdateModel(t);
    if (ModelState.IsValid)
    {
        db.SubmitChanges();
    }
    return View(t);
}
+1  A: 

You should re-query the original tableModel, map the updated row and then update.

Perhaps something like this (example only, not knowing anything about your schema):

var originalTableModel = db.GetById( tableModel.Id);
originalTableModel.FirstName = tableModel.FirstName;
db.SubmitChanges();
JcMalta
I have a lot of properties that are being changed, do I would really need to do this with all the properties? Seems like it would make model binding pointless. I could just assign the form values to the new model properties.
Dustin Laine
Also, I do not have a GetById method of my datacontext. Is this normal?
Dustin Laine
You should at least have a primary key ... (often "Id" but could be any other field that will uniquely identify a table row).For complex mapping, I would suggest looking at "AutoMapper" (http://automapper.codeplex.com/) which can eliminate a lot of the left-side/right-side assignment code.But perhaps get more comfotable with the workflow of LINQ CRUD operations using siimple tables, then investigate Automapper and Repository type patterns when you feel more comforable with the basics.
JcMalta
It is a simple table, and there is a primary key. I am not sure how that has anything to do with not updating? The properties including the ID are being bound from the view. The object itself is correct and should save just fine. However it is just not.
Dustin Laine
You "lost" the original DataContext on the return from your "GET"/Edit View ... by the time you get back to your "POST"/Edit .. that original DataContext no longer exists: so therefore cannot be used to "SubmitChanges".... So, we must create a new DataContext with the _original_ data, perform the changes and then allow this newly created DataConext to submit those changes.
JcMalta
Thanks, finally got it sorted and this last comment is the exact answer I needed. I am updating my question with my solution, but you had it right from the start.
Dustin Laine