简体   繁体   中英

MVC 5 update unknown columns in row

i need to make update in some columns in the row,but i didn't know which columns users will update it. there are many required fields, may be users didn't need to update some this required filed

         [HttpPost]
        public ActionResult Edit(int id,users user)
        {

                users query = db.users.Single(u => u.Id == id);


            query.username = user.username;
            query.password = user.password;
            query.confirmPassword = user.confirmPassword;
            query.email = user.email;
            query.type = user.type;
            query.photopath = user.photopath;
            query.address = user.address;
            query.note = user.note;

            db.SaveChanges();

                return RedirectToAction("Index");
}

by this way if i update some required filed there are an error

An exception of type 'System.Data.Entity.Validation.DbEntityValidationException' occurred in EntityFramework.dll but was not handled in user code

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

check for row which needs to be Updated by using if condition before Updating as in

if(!user.username.toString().Equals(query.username))//dont update it when already they are same
{
 query.username = user.username; 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM