Hello, I am building for that will let users update a table row. The table has 20+ coloumns (legacy deal) and I only want to allow the user to edit 4 colomns.
The standard MVC templates normally work great for this kind of thing, but I don't want to have 20 hidden form inputs (eg @Html.HiddenFor(model => model.EntityLevel) ). If no hidden inputs are there, they are treated blank when TryUpdateModel is called,
overwriting the existing values with nulls.
Is there a way to 'merge' the changes in a few rows with existing row in the db?
your code makes little sense. TryUpdateModel, binds the from post values to passed model, but you pass a model that has already been bound via it being a parameter.
//Controller
public ActionResult UpdateEntity(int id)
{
// passing id in hidden field - should really encrypt
var entity = db.Entity.Find(e => e.ID == id);
TryUpdateModel(entity);
if (ModelState.IsValid)
{
....
thorma04
Member
26 Points
51 Posts
Build a strongly-typed update form where only some values are changed.
Jan 03, 2013 06:50 PM|LINK
Hello, I am building for that will let users update a table row. The table has 20+ coloumns (legacy deal) and I only want to allow the user to edit 4 colomns.
//Form @Html.HiddenFor(model => model.EntityLevel) // x20 <div class="editor-label"> @Html.LabelFor(model => model.EntityName) @Html.EditorFor(model => model.EntityName) @Html.ValidationMessageFor(model => model.EntityName) </div> <div class="editor-label"> @Html.LabelFor(model => model.EntityEmail) @Html.EditorFor(model => model.EntityEmail) @Html.ValidationMessageFor(model => model.EntityEmail) </div> //Controller public ActionResult UpdateEntity(Entity entity) { TryUpdateModel(entity); if (ModelState.IsValid) { db.Entry(entity).State = EntityState.Modified; db.SaveChanges(); } return View(); }The standard MVC templates normally work great for this kind of thing, but I don't want to have 20 hidden form inputs (eg @Html.HiddenFor(model => model.EntityLevel) ). If no hidden inputs are there, they are treated blank when TryUpdateModel is called, overwriting the existing values with nulls.
Is there a way to 'merge' the changes in a few rows with existing row in the db?
Thanks!
bruce (sqlwo...
All-Star
36644 Points
5432 Posts
Re: Build a strongly-typed update form where only some values are changed.
Jan 03, 2013 06:59 PM|LINK
your code makes little sense. TryUpdateModel, binds the from post values to passed model, but you pass a model that has already been bound via it being a parameter.
//Controller public ActionResult UpdateEntity(int id) { // passing id in hidden field - should really encrypt var entity = db.Entity.Find(e => e.ID == id); TryUpdateModel(entity); if (ModelState.IsValid) { ....