How keep original value for some field when execute Edit on MVC?

.net c# entity-framework


As you know, When we want to Modify a data, We will go to Edit page:

public ActionResult EditAdmin(int UserId)
        User user = persons.Users.Find(id);
        return View(user);

Then We submit it on the Edit Page, it will Modify:

public ActionResult EditAdmin(User user)
        persons.Entry(user).State = EntityState.Modified;

But the problem is , I have alot of field don't need be modify:

public class User{
    public int UserId {get; set;} // do not need modify
    public int Password {get; set;} // do not need modify
    public string Name {get; set;}
    public bool Sex {get; set;}
    public DateTime AddTime {get; set;} // do not need modify

Obviously, I can't display some field on my edit page use Hidden, because I don't want it display on UI. but when submit, I still need it still keep the original value. So Is there any good idea for it? Thanks


Why I can't use like

entry.Property(e => e.Password).IsModified = false;


But It will display :

Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.

5/23/2017 12:26:23 PM

Accepted Answer

Fetch the existing version from the database, and then change just the 'modifiable' fields:

public ActionResult EditAdmin(User user)
    var currentPerson = db.Persons.FirstOrDefault(p => =;
    if (currentPerson == null)
        return HttpNotFound();

    currentPerson.Name = user.Name;
    currentPerson.Sex = user.Sex;
    // Id and Password are not updated.

  • You might also want to do some optimistic concurrency checking as well, to ensure that the version being updated, is in fact current. Ideally, if you have a timestamp, use this, otherwise, you are faced with comparing all fields.

See also @Kris' comment and Ric's point about creating tailored view models and hence NOT polluting your views with ORM / data tier entities. I also still contend you need to carry a timestamp or hash through the ViewModel to prevent the last one wins overwriting problem.

10/24/2014 3:39:15 PM

Popular Answer

Option 1:

You could use a readonly attribute:

Something like:

@Html.EditorFor(model => model.DriverID, new { htmlAttributes = new { 
        @Value = @Html.Action("getNextDriverID"), @readonly = "readonly"} })

Don't worry about the @Value part as this allows me to call an action method to auto generate a value.

In context, yours would look like:

@Html.EditorFor(model => model.UserId, new { htmlAttributes = new {@readonly = "readonly"} })

Please Note

This answer refers to using razor view engine.

Option 2:

Another option would be to use a different viewModel altogether:

public class edit User{

    public int userId {get; set;}
    public string Name {get; set;}
    public bool Sex {get; set;}

And then 'fill' your data using this in your `Edit ActionResult.

from there, you could then set the values in your [HttpPost] Action method using (linq or otherwise), before then saving to your database.

Option 3: Using ViewBags

since you are only looking to edit 2 parts of your model, you might just want to use the ViewBag:


ViewBag.Item1 = xyz;
ViewBag.Item2 = xyz;



Then in your post method, you could add these as string parameters:

public ActionResult Edit(string Item1, string Item2)

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow