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);
}