ASP.NET MVC 5:在视图和模型绑定中编辑多个记录(批量数据更新)

时间:2015-11-09 15:00:04

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

我需要更正以下代码。 我有两个班“员工”和“孩子”。 当我想创建一个新的员工时,我希望能够以相同的形式创建相关的孩子(最多2个孩子)。

以下是模型

public class Employee
{
    public int EmployeeID { get; set; }
    public string Name { get; set; }
    public string Surname { get; set; }

    public int ChildID { get; set; }

    public virtual ICollection<Child> Childs { get; set; }

}

public class Child
{
    public int ChildID { get; set; }
    public string NameChild { get; set; }
    public string SurnameChild { get; set; }

    public virtual Employee Employee { get; set; }
}

员工控制器

public class EmployeController : Controller
{
    private ComideContext db = new ComideContext();

    // GET: Employe/Create
    public ActionResult Create()
    {
        List<Child> model = new List<Child>();
        return View(model);
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create([Bind(Include = "EmployeID,Name,Surname,ChildID")] Employee employee)
    {
        if (ModelState.IsValid)
        {
            db.Employes.Add(employe);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        return View(employe);
    }
}

员工表格视图

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>Employe</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Surname, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Surname, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Surname, "", new { @class = "text-danger" })
            </div>
        </div>

        @for (int i=0; i<2; i++ )
        {
            <div class="form-group">
                @Html.LabelFor(model => model.NameChild, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.NameChild, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.NameChild, "", new { @class = "text-danger" })
                </div>
            </div>
            <div class="form-group">
                @Html.LabelFor(model => model.SurnameChild, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.SurnameChild, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.SurnameChild, "", new { @class = "text-danger" })
                </div>
            </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>
}

非常感谢任何帮助/想法。

谢谢。

1 个答案:

答案 0 :(得分:0)

免责声明:我在MVC 3上做过这个。我不知道在MVC 5中是否有更简单的方法。

您需要在视图代码中索引子项,以便在提交表单时,模型绑定器知道如何构造Employee对象。

这可以这样做:

 for (var i = 0 ; i < collectionSize; i++)
{
    @Html.EditorFor(child => Model.Childs[i].ChildID)
    @Html.ValidationMessageFor(child => Model.Childs[i].ChildID)
    [....]
}

这将要求在传递给视图时初始化您的集合。

您可以绑定到集合的另一种方法是以编程方式构建html组件的名称,以在列表中包含它们的索引。 请参阅以下示例:

<input name="Employee.Childs[0].ChildID" >
<input name="Employee.Childs[1].ChildID" >