实体框架中的MVC Viewmodel [英] MVC Viewmodel in entity framework

查看:88
本文介绍了实体框架中的MVC Viewmodel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有员工记录,而人事记录又包含地址记录。我已经设计了如下的viewmodel来填充三个表中的记录。



使用System;

使用System.Collections.Generic;

使用System.ComponentModel.DataAnnotations;

使用System.Linq;

使用System.Web;



命名空间Altus1.viewmodels

{

公共类EmployeeViewModel

{

公共员工员工{get;组; }

公共地址{get;组; }

public person person {get;组; }



私人AltusEntities context = new AltusEntities();



公共EmployeeViewModel(员工员工,人员) ,地址)

{

this.employee = employee;

this.person = person;

this .address = address;

}

public EmployeeViewModel()

{

this.employee = new Employee( );

this.person = new Person();

this.address = new Address();

}

}

}



请指导我如何在创建/编辑帖子中的这三个表中添加/更新记录动作。

I have employee record having person record which in turn contains address record. I have designed the viewmodel as below to populate the records from three tables.

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

namespace Altus1.viewmodels
{
public class EmployeeViewModel
{
public Employee employee { get; set; }
public Address address { get; set; }
public Person person { get; set; }

private AltusEntities context = new AltusEntities();

public EmployeeViewModel(Employee employee, Person person, Address address)
{
this.employee = employee;
this.person = person;
this.address = address;
}
public EmployeeViewModel()
{
this.employee = new Employee();
this.person = new Person();
this.address = new Address();
}
}
}

Please guide me as how can I add/update record in these three tables in the create/edit post action.

推荐答案

不要看和更新/编辑/删除模型。



1你应该创建你的更新相同的模型。



或在控制器操作l中处理此问题ike。



public ActionResult Action(Employee emp)

{

//检查模型状态这里

YourDatacontext context = new YourDatacontext();



员工empupdate =新员工();

empupdate.Name = emp.Name;

empupdate。 Age = emp.Age;



context.update(empupdate);



//处理异常



返回查看(YourView);

}
Dont see and Update/Edit/delete Model.

1st you should create your update model for same.

or handle this in controller action like.

public ActionResult Action(Employee emp)
{
//check model state here
YourDatacontext context=new YourDatacontext();

Employee empupdate=new Employee();
empupdate.Name=emp.Name;
empupdate.Age=emp.Age;

context.update(empupdate);

// handle exceptions

return View("YourView");
}


编辑很好..这里是代码..



公共ActionResult编辑(EmployeeViewModel employeeview)

{

ViewData [disablecontrols] =false;

if(ModelState.IsValid)

{

db.Entry(employeeview.employee).State = EntityState.Modified ;

db.Entry(employeeview.person).State = EntityState.Modified;

db.Entry(employeeview.address).State = EntityState.Modified;

员工employeeToSave = db.Employees.Find(employeeview.employee.id);

employeeToSave.Person = employeeview.person;

employeeToSave.Person.Address = employeeview.address;

db.SaveChanges();

返回RedirectToAction(Index );

}

ViewBag.EMP_WORKDEPT =新的SelectList(db.Departments,ID,DEPT_NAME,employeeview.employee.EMP_WORKDEPT);

ViewBag.EMP_DESIGNATION_ID = new SelectList(db.Designations,ID,DESIGN_NAME,employeeview.employee.EMP_DESIGNATION_ID);

ViewBag.EMP_LEVEL = new SelectList(db.EmployeeLevels, id,DESCRIPTION,employeeview.employee.EMP_LEVEL);

ViewBag.PER_SEX = new SelectList(db.Genders,id,DESCRIPTION,employeeview.person.PER_SEX);

ViewBag.ADDR_COUNTRY =新的SelectList(db.Countries,ID,COUNTRY1,employeeview.address.ADDR_COUNTRY);

ViewBag.ADDR_STATE = new SelectList(db.States ,id,State1,employeeview.addre ss.ADDR_STATE);

返回查看(employeeview);

}



问题是如何添加记录在单个SaveChanges调用数据上下文?
Edit is fine.. here is the code..

public ActionResult Edit(EmployeeViewModel employeeview)
{
ViewData["disablecontrols"] = "false";
if (ModelState.IsValid)
{
db.Entry(employeeview.employee).State = EntityState.Modified;
db.Entry(employeeview.person).State = EntityState.Modified;
db.Entry(employeeview.address).State = EntityState.Modified;
Employee employeeToSave = db.Employees.Find(employeeview.employee.id);
employeeToSave.Person= employeeview.person;
employeeToSave.Person.Address = employeeview.address;
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.EMP_WORKDEPT = new SelectList(db.Departments, "ID", "DEPT_NAME", employeeview.employee.EMP_WORKDEPT);
ViewBag.EMP_DESIGNATION_ID = new SelectList(db.Designations, "ID", "DESIGN_NAME", employeeview.employee.EMP_DESIGNATION_ID);
ViewBag.EMP_LEVEL = new SelectList(db.EmployeeLevels, "id", "DESCRIPTION", employeeview.employee.EMP_LEVEL);
ViewBag.PER_SEX = new SelectList(db.Genders, "id", "DESCRIPTION", employeeview.person.PER_SEX);
ViewBag.ADDR_COUNTRY = new SelectList(db.Countries, "ID", "COUNTRY1", employeeview.address.ADDR_COUNTRY);
ViewBag.ADDR_STATE = new SelectList(db.States, "id", "State1", employeeview.address.ADDR_STATE);
return View(employeeview);
}

The question is how to add records in a single SaveChanges call to data context?


这篇关于实体框架中的MVC Viewmodel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆