How to avoid certain fields to be updated in model using asp.net mvc 4

12,125
  1. You could manually set the fields that you are updating only and save the changes.

  2. You could reset the values taken from your database first. Something like the following (please forgive any errors)

    var userFromDb = db.Users.Where(u => u.UserID == user.UserID).First();
    user.Password = person.Password;
    user.VerifyPassword = person.VerifyPassword;
    
    db.Entry(person).CurrentValues.SetValues(user);
    db.SaveChanges();
    
  3. Create a custom model binder that only updates the relevant fields.

Share:
12,125
CodeWarrior
Author by

CodeWarrior

Updated on June 18, 2022

Comments

  • CodeWarrior
    CodeWarrior almost 2 years

    I am new to asp.net mvc 4 and EF. I am creating User Model which creates/updates user info. The code for model is as follows:

    ....
    public int UserID { get; set; }
    
    [Required]
    public string UserName { get; set; }
    
    [Required]
    public string Password { get; set; }
    
    [Compare("Password")]   
    public string VerifyPassword { get; set; }
    .....
     SOME OTHER FIELDS COMES HERE
    ......
    

    This same model used for create/update activity. This works fine for create user but while updating i don't want user to update his Id, username so I keep it readonly also don't want to show user password/verify password fields so I removed it from view. Now while updating(Edit mode) I am always getting validation error message for password field. So my question is how do I avoid updating these fields and do update for other fields. To solve this problem I try to create viewmodel but it doesn't help me. Also added code for bypassing validation like this in controller:

    public ActionResult Edit(User user)
    {
       ModelState.Remove("Password");
       ModelState.Remove("VerifyPassword");
       ModelState.Clear();
       if (ModelState.IsValid)
       {
         db.Entry(user).State = EntityState.Modified;
         db.SaveChanges();
         return RedirectToAction("Index");
       }
    }
    

    This give me ModelState.IsValid = true but fails to update. Please give me solution to this problem.

    Thanks in advance..

  • CodeWarrior
    CodeWarrior almost 11 years
    Thanks for reply. I am using entity framework for data access it auto generates all model classes like User ect. How do I create new Model as you said above. I have created User entity it shows User.tt under my .edmx file there is no option to add new model in that. Please tell me if I am wrong as I am new to MVC and EF.
  • dmusial
    dmusial almost 11 years
    The default approach is to create the models in the Models folder in your project. Simply add a new class in your Models folder, name it UserEditModel and add all the required code. You can use it without any problems in your views and controllers.
  • CodeWarrior
    CodeWarrior almost 11 years
    Thanks for reply. I done same thing as you said. By taking record by UserID and I set only those values that I want to update except password and other fields that I don't want to update. This works for me. Thanks.
  • stefan
    stefan over 8 years
    This helped ma also! Just a little improvement: Car c = db.Cars.Find(car.Id);
  • isaolmez
    isaolmez over 8 years
    Does not this make another database call to get original values for the to be updated user?