2014-03-14 45 views
0

獲取數據我有2個表tbl_computertbl_computerperipheral 我需要它由兩個表數據的編輯器視圖。從兩個表

如何在單個視圖中獲取2個表格,以便我可以一次將數據插入到2個表格中。

感謝名單

+0

你從兩個表中獲取數據,並在單一視圖填充?是或否 – JegsVala

+0

您好,歡迎來到StackOverflow。我不認爲你的問題很清楚。請儘可能詳細地描述你的問題。請閱讀[我如何問一個好問題?](http://stackoverflow.com/help/how-to-ask)和[編寫完美問題](http://tinyurl.com/so-hints)。 –

回答

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); 
    } 
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(); 
    } 

可能是這個答案將有助於獲得您的查詢的答案。

+0

Thanx回覆Rahul RJ ..當我使用上面的代碼,我無法獲得數據。代碼FirstModel = common.FirstModel不會提供所有數據。我需要逐個添加所有字段。有沒有其他方法 – user3405577