2015-10-13 124 views
0

我是新來的ASP.NET mvc,我有疑問。如何將數據插入多對多關係表的橋表中。如何將數據插入多對多的關係表asp.net mvc

我有兩個模型,如 class.cs和student.cs

public class Class 
{ 
public int classId {get; set;} 
public string classname {get; set;} 
public virtual List<Student> Class {get; set;} 
} 

public class Student 
{ 
public int studentId {get; set;} 
public string studentname {get; set;} 
public virtual List<Class> Class {get; set;} 
} 

//data layer 
public class ClassConfiguration : EntityTypeConfiguration<Class> 
{ 
public ClassConfiguration() 
{ 
HasMany(c => c.Student).WithMany(c => c.Class).Map(c => {c.MapLeftKey("classId"); 
c.MapRightKey("studentId"); 
c.ToTable("ClassStudentTable"); 
}); 
} 
} 

//I can see ClassStudentTable in database not in entities. 

//Mapping between these two 
public class ApplicationDbContext : IdentityDbContext<ApplicationUser> 
{ 
public ApplicationDbContext() : base("DefaultConnection", throwIfV1Schema: false) 
public DbSet<Class> Class {get; set;} 
public DbSet<Student> Student {get; set;} 

protected override void OnModelCreating(DbModleBuilder modelBuilder) 
{ 
modelBuilder.Configurations.Add(new ClassConfiguration()); 

base.OnModelCreating(modelBuilder); 
} 

我有兩個表類和學生用表橋ClassStudentTable。 使用默認的MVC scaffolding-爲學生

創建方法
//http:get for student 
public ActionResult Create() 
{ 
ViewBag.Class = new SelectList(db.Class, "classId", "classname"); 
return View(); 
} 

//http:post for student 
[HttpPost] 
[ValidateAntiForgeryToken] 
public ActionResult Create(Bind(Include = "studentId,studentname") Student student) 
{ 
if(ModelState.IsValid) 
db.Student.Add(student); 
db.SaveChanges(); 
return RedirectToAction("Index"); 
} 

現在查看創建方法是

@model TestProject.Models.Student 
@{ 
ViewBag.Title = "Create"; 
} 
@using (Html.BeginForm()) 
{ 
@Html.AntiForgeryToken() 
@Html.ValidationSummary(true, "", new { @class = "text-danger" }) 
<div class="form-horizontal"> 
<div class="form-group"> 
    @Html.LabelFor(model => model.studentname, htmlAttributes: new {@class = "control-label col-md-2"}) 
    <div class="col-md-10"> 
    @Html.EditorFor(model => model.studentname, new { htmlAttributes = new {@class ="form-control"}}) 
    @Html.ValidateMessengerFor(model => model.studentname, "", new {@class = "text-danger"}) 
    </div> 
</div> 

<table class="table" id="tbClass"> 
<tr class="tr-header"> 
    <th>Class</th> 
    <th><a href="javascript:void(0);" id="addMore"></th> 
</tr> 

<tr> 
    <td> 
    <div class="col-md-10"> 
    @Html.DropDownList("Class", null, htmlAttributes: new { @class = "control-label col-md-2" }) 
    @Html.ValidationMessageFor(model => model.Class, "", new { @class = "text-danger" }) 
    </div> 
    </td> 
    <td><a href='javascript:void(0);' class='remove'><span class='glyphicon glyphicon-remove'></span></a></td> 
</tr> 
</table> 

<div class="form-group"> 
<input type="submit" value="Create" class="btn btn-success"> 
@Html.ActionLink("Cancel", "Index", null, new {@class = "btn btn-default"}) 
</div> 
</div> 
} 

<script> 
$(function() { 
    $('#addMore').on('click', function() { 
     var data = $("#tbClass tr:eq(1)").clone(true).appendTo("#tbClass"); 
     data.find("input").val(''); 
    }); 

    $(document).on('click', '.remove', function() { 
     var trIndex = $(this).closest("tr").index(); 
     if (trIndex > 1) { 
      $(this).closest("tr").remove(); 
     } else { 
      alert("Sorry!! Should have atleast on row!"); 
     } 
    }); 
}); 

我現在正在爲這一切的源代碼文件。

現在我的問題很清楚。

當我添加新的動態行到表中向學生添加新班級(顯示爲學生選擇班級的下拉列表)。我可以爲前面的學生選擇儘可能多的班級,但我如何通過值來創建()[HTTPPOST]。

我可以通過(List類)作爲[參數] ??如果是的話,我該如何將表格數據轉換成List。

在你的代碼等待回覆

+0

不清楚你想要做什麼。你的用戶界面沒有多大意義。如果你想動態地添加新的項目到集合然後參考答案[這裏](http://stackoverflow.com/questions/29161481/post-a-form-array-without-successful/29161796#29161796)和[在這裏](http://stackoverflow.com/questions/28019793/submit-same-partial-view-called-multiple-times-data-to-controller/28081308#28081308)但在這種情況下,我懷疑你真的只想要一個複選框列表,以便您可以選擇學生所關聯的班級。 –

+0

但我有數百班。我不能把所有的課程放在cleckbox中。我希望用戶動態地向特定學生添加類 - Stephen Muecke –

+0

然後,您可以隨時使用ListBox或jquery插件(例如[tagit](http://aehlke.github.io/tag-it/))。你當前的實現允許用戶選擇一個類,添加一個新行,然後再次選擇同一個類,但是如果你想使用這個實現,請閱讀我在第一條評論中給出的鏈接 –

回答

0

寫您現在使用1對多的關係。你應該改變你的關係到很多很多。實現多對多關係try this link

相關問題