asp.net mvc custom model binding in an update entity scenario


asp.net mvc custom model binding in an update entity scenario



Hi I have a question about model binding. Imagine you have an existing database entity displayed in a form and you'd like to edit some details, some properties eg createddate etc are not bound to the form, during model binding, these properties are not assigned to the model as they are not on the http post data or querystrong etc, hence their properties are null. In my controller method for update , Id just like to do

public ActionResult Update( Entity ent) { //Save changes to db }

but as some properties are null in ent, they override the existing database fields which are not part of the form post data, What is the correct way to handle this? Ive tried hidden fields to hold the data, but model binding does not seem to assign hidden fields to the model. Any suggestions would be appreciated




ajax.actionlink

1:



Is ADO.NET Entity Framework (with ASP.NET MVC v2) a viable option when writing custom and contantly updated websites?
You shouldn't be sending your entity to your view, you should send a slim version of it called a DTO (data transfer object) or ViewModel..
How to disable VS compile warning “class or css class is not defined”
Only send the properties to the view (and hence the form that gets posted back) that you want the user to update with that action..
patterns to use when building complex web UI
In your POST handler you can then validate and copy the properties across to your entity..
Validation not firing even when my form is obviously invalid
an AutoMapper can help if you have lots of properties.
asp.net mvc datannotation password validate
something like:.
ASP.NET MVC - How controls retain their state
public class User {     int id;     string name;     string email; }  public class EditUserEmailDto {     string email; }  // get public ActionResult EditEmail(int id,) {     return View("EditEmail", new EditUserEmailDto()); }      // post public ActionResult EditEmail(int id, EditUserEmailDto dto) {     if(!ModelState.IsValid)         return View("EditEmail", dto);      var user = userRepo.Get(id);     user.email = dto.email;     userRepo.Save(user);      return; } 


JavaScript url auto-resolution in Asp.Net MVC


52 out of 100 based on 37 user ratings 612 reviews