【发布时间】:2018-06-11 21:39:19
【问题描述】:
员工模型
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; }
}
部门模型
public class Department
{
public int DepartmentID { get; set; }
public string DepartmentName { get; set; }
}
部门和员工的视图模型
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; }
}
现在我想用单个视图更新两个表。
控制器
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");
}
但是在这个方法中只更新员工记录而不是部门
【问题讨论】:
-
看看这个可能对你有帮助。technotipstutorial.blogspot.com/2016/12/…
-
你能告诉我们一些你已经尝试过的失败吗?
-
感谢您的评论,请再次检查代码我更新它:)
标签: c# asp.net-mvc entity-framework-6 viewmodel asp.net-mvc-viewmodel