MVC主细节更新

时间:2014-01-15 16:24:32

标签: asp.net-mvc entity-framework

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

主表:人

详细信息表:PersonPhoneNumber

我需要在db.SaveChanges()

之前更新PersonPhoneNumbers

模型

public class Person
{
    public Person()
    {
        this.PersonPhoneNumbers = new HashSet<PersonPhoneNumber>();
    }
    public int PersonId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }

    public virtual ICollection<PersonPhoneNumber> PersonPhoneNumbers { get; set; }        

    public void BuildPhoneNumbers(int count = 1)
    {
        for (int i = 0; i < count; i++)
        {
            PersonPhoneNumbers.Add(new PersonPhoneNumber());
        }
    }
}

public class PersonPhoneNumber
{
    public int PersonPhoneNumberId { get; set; }
    public string PhoneNumber { get; set; }
    public int PersonId { get; set; }
    public bool DeletePhone { get; set; }
}

控制器

// GET: /Person/Edit/5
    public ActionResult Edit(int? id)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        Person person = db.Persons.Find(id);            
        if (person == null)
        {
            return HttpNotFound();
        }
        return View(person);
    }

    // POST: /Person/Edit/5
    // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
    // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Edit(Person person)
    {
        if (ModelState.IsValid)
        {
            db.Entry(person).State = EntityState.Modified;
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        return View(person);
    }

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>
}

EditorTemplates 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>

另外请告诉我有没有更好的方法来做同样的事情。 感谢

1 个答案:

答案 0 :(得分:0)

试试这个:

[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);
}