如何在一个表中插入多行? (MVC 5 ASP.NET)

时间:2016-09-02 16:33:10

标签: c# asp.net asp.net-mvc asp.net-mvc-4

我想在我的表中使用它的Create Method(我使用Scaffolding方法自动生成)在我的表中插入多个条目,称为'sibling'。我已经在添加字段时使表单动态化了。但是,当我点击“提交”按钮时,它只会获得第一个条目。

我有点想通过在所述方法中实现'foreach',但我似乎无法找到一个现实的答案。特别是对于我的情况,我只想修改自动生成的'兄弟'的创建方法(如果有这样的方法而不必创建新方法 - 但如果不是,我也可以采用不同的方法)。 / p>

这是我的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);
    }

这是我的兄弟姐妹的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>

这是我的'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; }
}

请帮忙。谢谢!

1 个答案:

答案 0 :(得分:0)

您可以在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();
}