.Net MVC Add Remove&在父视图中删除子项(一对多关系)

时间:2017-02-08 15:25:49

标签: asp.net-mvc

我的项目遇到了一些麻烦。我在部门和数字表之间有一对多的关系。一个部门可以有0个或多个号码。

enter image description here

在我的部门模型中,我有一个ICollection数字,我正在寻找修改Numbers集合的最佳方法。

这是我的代码看起来像:

型号:

    [Table("Department")]
public partial class Department
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
    public Department()
    {
        Numbers = new List<Number>();
    }

    public int Id { get; set; }

    [Required]
    [StringLength(255)]
    public string Name { get; set; }

    [StringLength(255)]
    public string Description { get; set; }

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<Number> Numbers { get; set; }
}

}

控制器:

        [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Edit([Bind(Include = "Id,Name,Description, Numbers")] Department department)
    {
        if (ModelState.IsValid)
        {
            foreach (Number nr in department.Numbers)
            {
                db.Entry(nr).State = EntityState.Modified;
            }
            db.Entry(department).State = EntityState.Modified;

            db.SaveChanges();
            return RedirectToAction("Index");
        }
        return View(afdeling);
    }

查看=编辑页面:

@model StandbyBeheer.Models.Afdeling

@{
    ViewBag.Title = "Edit";
}

<div>
    @Html.ActionLink("Terug", "Index")
</div>
<h2>Afdeling bewerken</h2>

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
    
    <div class="form-horizontal">
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        @Html.HiddenFor(model => model.Id)

        <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.Description, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Description, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Description, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.EditorFor(model => model.Numbers)
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Opslaan" class="btn btn-primary" />
            </div>
        </div>
    </div>
}

我有一个这样的EditorTemplate:

    @model StandbyBeheer.Models.Number
<div class="form-group">
    @Html.HiddenFor(model => model.Id)
    @Html.TextBoxFor(model => model.PhoneNumber, new { @class = "form-control text-box single-line" })
</div>

我可以看到我部门编辑页面中的数字,并更改它们但是当我提交表单时,我收到错误:“InvalidOperationException未被用户代码处理”

我如何解决这个问题?在我的收藏中添加/编辑/删除数字的最佳方法是什么?我的想法是用AJAX创建文本字段,但是我仍然有问题将文本字段值添加到我的集合中。我希望有一个人可以帮助我!提前谢谢!

0 个答案:

没有答案