Asp.net mvc MVC主细节更新

Asp.net mvc MVC主细节更新,asp.net-mvc,entity-framework,Asp.net Mvc,Entity Framework,我已经用editorfor模板创建了主详细信息表单,我可以添加新记录,但不知道如何在编辑时更新子记录。我可以在控制器中看到电话号码 主表:人 明细表:PersonPhoneNumber 我需要在db.SaveChanges()之前更新PersonPhoneNumber 模型 Edit.cshtml @using (Html.BeginForm()) { @Html.AntiForgeryToken() <div class="form-horizontal"> <h4

我已经用editorfor模板创建了主详细信息表单,我可以添加新记录,但不知道如何在编辑时更新子记录。我可以在控制器中看到电话号码

主表:人

明细表:PersonPhoneNumber

我需要在db.SaveChanges()之前更新PersonPhoneNumber

模型

Edit.cshtml

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

<div class="form-horizontal">
    <h4>Person</h4>
    <hr />
    @Html.ValidationSummary(true)
    @Html.HiddenFor(model => model.PersonId)

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

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

    <div>
        <label>City</label>
        @Html.EditorFor(model => model.PersonPhoneNumbers)
    </div>

    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Save" class="btn btn-default" />
        </div>
    </div>
</div>
}
@使用(Html.BeginForm())
{
@Html.AntiForgeryToken()
人

@Html.ValidationSummary(true) @Html.HiddenFor(model=>model.PersonId) @LabelFor(model=>model.FirstName,新的{@class=“controllabel col-md-2”}) @EditorFor(model=>model.FirstName) @Html.ValidationMessageFor(model=>model.FirstName) @LabelFor(model=>model.LastName,新的{@class=“controllabel col-md-2”}) @EditorFor(model=>model.LastName) @Html.ValidationMessageFor(model=>model.LastName) 城市 @EditorFor(model=>model.PersonPhoneNumber) }
编辑模板 PersonPhoneNumber.cshtml

<div>
@Html.HiddenFor(model => model.PersonPhoneNumberId)
@Html.HiddenFor(model => model.PersonId)
</div>
<div class="form-group">
@Html.LabelFor(model => model.PhoneNumber, new { @class = "control-label col-md-2" })
<div class="col-md-10">
    @Html.EditorFor(model => model.PhoneNumber)
    @Html.ValidationMessageFor(model => model.PhoneNumber)
</div>
</div>

@Html.HiddenFor(model=>model.PersonPhoneNumberId)
@Html.HiddenFor(model=>model.PersonId)
@LabelFor(model=>model.PhoneNumber,新的{@class=“controllabel col-md-2”})
@EditorFor(model=>model.PhoneNumber)
@Html.ValidationMessageFor(model=>model.PhoneNumber)
另外,请让我知道有没有更好的方法做同样的事情。 谢谢

试试这个:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(Person person)
{
    if (ModelState.IsValid)
    {
        db.Entry(person).State = EntityState.Modified;

        foreach (var phoneNumber in person.PersonPhoneNumbers)
            db.Entry(phoneNumber).State = EntityState.Modified;

        db.SaveChanges();

        return RedirectToAction("Index");
    }
    return View(person);
}
<div>
@Html.HiddenFor(model => model.PersonPhoneNumberId)
@Html.HiddenFor(model => model.PersonId)
</div>
<div class="form-group">
@Html.LabelFor(model => model.PhoneNumber, new { @class = "control-label col-md-2" })
<div class="col-md-10">
    @Html.EditorFor(model => model.PhoneNumber)
    @Html.ValidationMessageFor(model => model.PhoneNumber)
</div>
</div>
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(Person person)
{
    if (ModelState.IsValid)
    {
        db.Entry(person).State = EntityState.Modified;

        foreach (var phoneNumber in person.PersonPhoneNumbers)
            db.Entry(phoneNumber).State = EntityState.Modified;

        db.SaveChanges();

        return RedirectToAction("Index");
    }
    return View(person);
}