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。
在你的代码等待回复
不清楚你想要做什么。你的用户界面没有多大意义。如果你想动态地添加新的项目到集合然后参考答案[这里](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)但在这种情况下,我怀疑你真的只想要一个复选框列表,以便您可以选择学生所关联的班级。 –
但我有数百班。我不能把所有的课程放在cleckbox中。我希望用户动态地向特定学生添加类 - Stephen Muecke –
然后,您可以随时使用ListBox或jquery插件(例如[tagit](http://aehlke.github.io/tag-it/))。你当前的实现允许用户选择一个类,添加一个新行,然后再次选择同一个类,但是如果你想使用这个实现,请阅读我在第一条评论中给出的链接 –