我有2个表 tbl_computer 和 tbl_computerperipheral 我需要一个编辑器视图,其中包含来自两个表的数据。
如何在单个视图中获取2个表,以便我可以一次将数据插入2个表中。
感谢名单
答案 0 :(得分:0)
为了更好的参考,请查看::
How to Combine two models into a single model and pass it to view using asp.net MVC razor
然后在服务器端的表单提交(即进入控制器的操作)中保存来自视图的数据,如::
public ActionResult Save(CommonViewModel common)
{
var FirstModel = new FirstModel();
FirstModel = common.FirstModel;
db.Entry(FirstModel).State = EntityState.Added;
var SecondModel = new SecondModel();
SecondModel = common.SecondModel;
db.Entry(SecondModel).State = EntityState.Added;
db.SaveChanges();
}
这个答案可能有助于您回答查询。
答案 1 :(得分:0)
[HttpPost]
public ActionResult Edit(CompPeripheral cp, int c_id,int em_id,int asset_id)
{
if (ModelState.IsValid)
{
cp.Compconfig.c_id = c_id;
cp.Compconfig.em_id = em_id;
cp.Compconfig.asset_id = asset_id;
db.tbl_compconfig.Add(cp.Compconfig);
db.SaveChanges();
var id = db.tbl_compconfig.Max(a => a.comp_id);
cp.Comperipheral.comp_id = id;
//saving Comp Peripheral in database
db.tbl_comperipheral.Add(cp.Comperipheral);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.asset_id = new SelectList(db.tbl_assetm, "asset_id", "asset_name", cp.Compconfig.asset_id);
ViewBag.em_id = new SelectList(db.tbl_employee, "em_id", "em_fullname", cp.Compconfig.em_id);
ViewBag.c_id = new SelectList(db.tbl_client, "c_id", "c_name", cp.Compconfig.c_id);
return View(cp);
}