2014-09-26 66 views
0

嗨,謝謝大家的幫助。我對ASP.net MVC和EF非常陌生,並且正在玩弄一些真正的項目。我真的很感激,如果有人可以請指導我如何在一對多關係中將數據插入到SQL中的兩個表中。 我有一個需求,我需要將多個家庭添加到一個行爲案例。ASP.net MVC 5將數據添加到多個表

  • tblCase [CASE_ID] [case_no]
  • tblFamily [family_id] [FIRST_NAME] [姓氏] [CASE_ID]

下一個在線教程後,我能夠添加多個家庭,但我可以不能讓我圍繞着如何頭:

  1. 添加新的案例 - >檢索最後插入CASE_ID
  2. 將所有與上次在家庭在一個提交按鈕單擊中選擇case_id。

我的看法如下所示:

@model List<TestMVC.Family> 

@{ 
ViewBag.Title = "BulkData"; 
Layout = "~/Views/Shared/_Layout.cshtml"; 
} 

<style> 
td { 
    padding: 5px; 
} 
</style> 
<div style="width:700px; padding:5px; background-color:white;"> 
@using (Html.BeginForm("BulkData", "Save", FormMethod.Post)) 
{ 
    @Html.AntiForgeryToken() 
    @Html.ValidationSummary(true) 

    if (ViewBag.Message != null) 
    { 
     <div style="border:solid 1px green"> 
      @ViewBag.Message 
     </div> 
    } 

    <div id="dvCase"> 
     <table> 
      <tr> 
       <td>Case Name</td> 
       <td><input type="text" value="" /></td> 
      </tr> 
      <tr> 
       <td>Case No</td> 
       <td><input type="text" value="" /></td> 
      </tr> 
     </table> 
    </div> 

    <div><a href="#" id="addNew">Add New</a></div> 
    <table id="dataTable" border="0" cellpadding="0" cellspacing="0"> 
     <tr> 
      <th>First Name</th> 
      <th>Last Name</th> 
      <th>Case Id</th> 
      <th></th> 
     </tr> 
     @if (Model != null && Model.Count > 0) 
     { 
      int j = 0; 
      foreach (var i in Model) 
      { 
       <tr style="border:1px solid black"> 
        <td>@Html.TextBoxFor(a => a[j].firstName)</td> 
        <td>@Html.TextBoxFor(a => a[j].lastName)</td> 
        <td>@Html.TextBoxFor(a => a[j].case_id)</td> 
        <td> 
         @if (j > 0) 
         { 
          <a href="#" class="remove">Remove</a> 
         } 
        </td> 
       </tr> 


         j++; 
      } 
     } 
    </table> 
    <input type="submit" value="Save" /> 
} 
</div> 

和我的控制器看起來像:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.Mvc; 

namespace TestMVC.Controllers 
{ 
public class SaveController : Controller 
{ 
    // 
    // GET: /Save/ 
    public ActionResult BulkData() 
    { 
Case bc = new Case {case_id=0,case_no=""};   
// This is only for show by default one row for insert data to the database 
     List<Family> ci = new List<Family> { new Family { family_id = 0, firstName = "", lastName = "", case_id= 0 } }; 
     return View(ci); 
    } 

    [HttpPost] 
    [ValidateAntiForgeryToken] 
    public ActionResult BulkData(List<Family> ci) 
    { 
     if (ModelState.IsValid) 
     { 
      using (FamilyContext dc = new FamilyContext()) 
      { 
       var behaveCase = new Case(); 
       behaveCase.case_no = "1234"; 
       dc.Cases.add(behaveCase); 
       foreach (var i in ci) 
       { 
        i.Case = behaveCase;      
        dc.Families.Add(i); 
       } 
       dc.SaveChanges(); 
       ViewBag.Message = "Data successfully saved!"; 
       ModelState.Clear(); 
       ci = new List<Family> { new Family { family_id = 0, firstName = "", lastName = "", case_id = 0 } }; 
      } 
     } 
     return View(ci); 
    } 
    } 
} 

和我的模型定義

namespace TestMVC 
{ 
using System; 
using System.Collections.Generic; 

public partial class Case 
{ 
    public Case() 
    { 
     this.Families = new HashSet<Family>(); 
    } 

    public int case_id { get; set; } 
    public string case_no { get; set; } 

    public virtual ICollection<Family> Families { get; set; } 
} 
} 

namespace TestMVC 
{ 
using System; 
using System.Collections.Generic; 

public partial class Family 
{ 
    public int family_id { get; set; } 
    public string firstName { get; set; } 
    public string lastName { get; set; } 
    public int case_id { get; set; } 

    public virtual Case Case { get; set; } 
} 
} 
+0

向我們展示您的模型定義 – py3r3str 2014-09-26 11:44:48

回答

0

模型,它補充說,是'我'在這裏:dc.Families.Add(i);當dc.SaveChanges()稱它會修改'i'的主鍵爲:

if (ModelState.IsValid) 
    { 
     using (FamilyContext dc = new FamilyContext()) 
     { 
      var behaveCase = new Case(); 
      behaveCase.case_no = "1234"; 
      dc.Cases.add(behaveCase); 
      dc.SaveChanges(); // Save Changes Occurred. primary key in inserted in behaveCase 
      int pk = behaveCase.case_id; // You can get primary key of your inserted row 
      foreach (var i in ci) 
      { 
       i.Case = behaveCase;      
       dc.Families.Add(i); 
       dc.SaveChanges(); // Save Changes Occured. Primary key of i is inserted 
       int pkFam = i.family_id; // You can get primary key of your inserted row 
      } 

      ViewBag.Message = "Data successfully saved!"; 
      ModelState.Clear(); 
      ci = new List<Family> { new Family { family_id = 0, firstName = "", lastName = "", case_id = 0 } }; 
     } 
    }