Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

People also ask

What is partial view in ASP NET MVC?

A partial view is a Razor markup file ( . cshtml ) without an @page directive that renders HTML output within another markup file's rendered output. The term partial view is used when developing either an MVC app, where markup files are called views, or a Razor Pages app, where markup files are called pages.

What is CRUD operations in MVC?

CRUD operation in MVC is the basic operations, where CRUD denotes create, read, update, and delete. But before understanding the basic operations of MVC, first, learn about MVC. MVC is the Model View Controller. MVC is a design pattern that is used to differentiate the data from business logic and presentation logic.


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;
        persons.SaveChanges();
}

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

Update1:

Why I can't use like

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

link: https://stackoverflow.com/a/18004476/1900498

But It will display :

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