how i update multiple table using viewmodel in Asp.net mvc

asp.net-mvc asp.net-mvc-viewmodel c# entity-framework-6 viewmodel

Question

Employee Model

public class Employee
    {
        [Key]
        public int EmployeeID { get; set; }
        public string Name { get; set; }
        public virtual Department Departments { get; set; }
        public int DepartmentID { get; set; }
    }

Department Model

public class Department
    {
        public int DepartmentID { get; set; }
        public string DepartmentName { get; set; }
    }

View Model for Department and Employee

public class EDViewModel
    {
        public int ID { get; set; }
        public int EmployeeID { get; set; }
        public string Name { get; set; }
        public Department Departments { get; set; }
        public int DepartmentID { get; set; }
        public string DepartmentName { get; set; }
    }

Now i want to update both tables with single view.

Controller

public ActionResult Edit(int?id)
        {
// write some code for update both table at once time
}

PostMethod

 [HttpPost]
        public ActionResult Edit(EDViewModel Emodel)
        {
            var user = db.Employees.Where(c => c.Employee_Id == Emodel.Employee_Id).FirstOrDefault();
            user.UserName = Emodel.UserName;
            user.ProfilePicture = Emodel.ProfilePicture;
            db.Entry(user).State = EntityState.Modified;
            db.Entry(user).State = EntityState.Modified;
            db.SaveChanges();
            return RedirectToAction("Home");
        }

But in this method only Update Employee record not department

1
0
6/11/2018 10:12:30 PM

Popular Answer

it worked for me just remove this

 db.Entry(user).State = EntityState.Modified;

if we not remove this entityvalidation occur

0
6/11/2018 10:58:45 PM


Related Questions





Related

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