asp.net mvc 2 - Model binding issue in LINQ to SQL -
i starting work linq, , pretty familiar mvc. have typed view updating record. have done creation:
this works fine, , creates record in database:
public actionresult create(tablemodel tablemodel) { dbdatacontext db = new dbdatacontext(); if (modelstate.isvalid) { db.tablemodel.insertonsubmit(tablemodel); db.submitchanges(); } }
but when trying update:
public actionresult manage(tablemodel tablemodel) { dbdatacontext db = new dbdatacontext(); if (modelstate.isvalid) { db.submitchanges(); } }
this fails, in sense not update record in database. no actual error/exception occurs, , can step through fine.
i sure missing something, cannot find what. appreciate on matter.
update
did notice if record using datacontext:
dbdatacontext db = new dbdatacontext(); var m = db.tablemodels.single(m => m.id == 1); m.name = "updatedname"; db.submitchanges();
this update, assume somehow not binding model linq context.
my solution
found need retrieve object , update 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); }
you should re-query original tablemodel, map updated row , update.
perhaps (example only, not knowing schema):
var originaltablemodel = db.getbyid( tablemodel.id); originaltablemodel.firstname = tablemodel.firstname; db.submitchanges();
Comments
Post a Comment