[英]How to Insert Multiple Rows in One Table? (MVC 5 ASP.NET)
I would want to insert multiple entries in my table called 'sibling' using it's Create Method (which I generated automatically using Scaffolding methods). 我想在我的表中插入多个条目,称为'sibling',使用它的Create Method(我使用Scaffolding方法自动生成)。 I already made the form dynamic in adding the fields.
我已经在添加字段时使表单动态化了。 However, when I click on the Submit button it only gets the first entry.
但是,当我点击“提交”按钮时,它只会获得第一个条目。
I sort of have an idea by implementing 'foreach' in the said method but I can't seem to find an answer that is spot on. 通过在上述方法中实现'foreach',我有点想法,但我似乎无法找到一个现场的答案。 Especially for my case I just want to modify the Create Method for 'sibling' which was generated automatically (if there is such a way without having to create a new method - but if not I am fine with a different approach as well).
特别是对于我的情况,我只想修改自动生成的'兄弟'的创建方法(如果有这样的方法而不必创建新方法 - 但如果不是,我也可以采用不同的方法)。
This is my SiblingController: 这是我的SiblingController:
public ActionResult Create(int id)
{
var sib = new sibling();
sib.child_id = id;
return View(sib);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "sibling_id,child_id,age,gender")] sibling sibling)
{
if (ModelState.IsValid)
{
db.siblings.Add(sibling);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.child_id = new SelectList(db.children, "child_id", "last_name", sibling.child_id);
return View(sibling);
}
This is my Create.cshtml for Sibling: 这是我的Sibling的Create.cshtml:
@model dummyApp.Schema.TestModels.sibling
@{
ViewBag.Title = "Create";
}
@section Scripts{
<script type="text/javascript">
//Coding
$("#numBox").change(function () {
var htmlString = "";
var len = $(this).val();
for (var i = 0; i < len; i++) {
htmlString += ' <div class="form-group">\
@Html.LabelFor(model => model.child_id, "child_id", htmlAttributes: new { @class = "control-label col-md-2" })\
<div class="col-md-10">\
@Html.EditorFor(model => model.child_id, new { htmlAttributes = new { @class = "form-control" }})\
@Html.ValidationMessageFor(model => model.child_id, "", new { @class = "text-danger" })\
</div>\
</div>\
<div class="form-group">\
@Html.LabelFor(model => model.age, htmlAttributes: new { @class = "control-label col-md-2" })\
<div class="col-md-10">\
@Html.EditorFor(model => model.age, new { htmlAttributes = new { @class = "form-control" } })\
@Html.ValidationMessageFor(model => model.age, "", new { @class = "text-danger" })\
</div>\
</div>\
<div class="form-group">\
@Html.LabelFor(model => model.gender, htmlAttributes: new { @class = "control-label col-md-2" })\
<div class="col-md-10">\
@Html.EditorFor(model => model.gender, new { htmlAttributes = new { @class = "form-control" } })\
@Html.ValidationMessageFor(model => model.gender, "", new { @class = "text-danger" })\
</div>\
</div>\
';
}
$("#adtnl_fields").html(htmlString);
})
</script>
}
<h2>Create</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>sibling</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<!--
<div class="form-group">
@Html.LabelFor(model => model.sibling_id, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.sibling_id, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.sibling_id, "", new { @class = "text-danger" })
</div>
</div>
-->
<div class="form-group">
@Html.Label("Number of Siblings:", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
<input type="number" id="numBox" class="form-control"/>
</div>
</div>
<!--
<div class="form-group">
@Html.LabelFor(model => model.child_id, "child_id", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@*Html.DropDownList("child_id", null, htmlAttributes: new { @class = "form-control" })*@
@Html.EditorFor(model => model.child_id, new { htmlAttributes = new { @class = "form-control" }})
@Html.ValidationMessageFor(model => model.child_id, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.age, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.age, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.age, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.gender, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.gender, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.gender, "", new { @class = "text-danger" })
</div>
</div>
-->
<!-- Div for additional fields -->
<div id="adtnl_fields">
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
This is my 'sibling.cs': 这是我的'sibling.cs':
public partial class sibling
{
public int sibling_id { get; set; }
public int child_id { get; set; }
public Nullable<int> age { get; set; }
public string gender { get; set; }
public virtual child child { get; set; }
}
Please help. 请帮忙。 Thank you!
谢谢!
You can use AddRange method in EF-6 您可以在EF-6中使用AddRange方法
IList<Student> newStudents = new List<Student>();
newStudents.Add(new Student() { StudentName = "Student1 by addrange" });
newStudents.Add(new Student() { StudentName = "Student2 by addrange" });
newStudents.Add(new Student() { StudentName = "Student3 by addrange" });
using (var context = new SchoolDBEntities())
{
context.Students.AddRange(newStudents);
context.SaveChanges();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.