How to avoid certain fields to be updated in model using asp.net mvc 4
You could manually set the fields that you are updating only and save the changes.
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();
Create a custom model binder that only updates the relevant fields.
CodeWarrior
Updated on June 18, 2022Comments
-
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 almost 11 yearsThanks 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 almost 11 yearsThe default approach is to create the models in the
Models
folder in your project. Simply add a new class in yourModels
folder, name itUserEditModel
and add all the required code. You can use it without any problems in your views and controllers. -
CodeWarrior almost 11 yearsThanks 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 over 8 yearsThis helped ma also! Just a little improvement: Car c = db.Cars.Find(car.Id);
-
isaolmez over 8 yearsDoes not this make another database call to get original values for the to be updated user?