MVC / EF部分更新相关数据

时间:2012-07-13 07:49:18

标签: asp.net-mvc entity-framework entity-relationship partial master-detail

我正试图了解相关数据,以便在MVC中使用EF进行主细节方案。搜索StackOverflow让我有了很大的帮助,但是在保存数据的过程中遇到了问题。作为StackOverflow上的更多示例,我使用人员/地址设置,以及人员和地址(一对多)我只在视图中使用部分实体属性。使用默认的脚手架会导致错误,但对于我设法使用下面的代码解决的人员实体。但对于Address实体,我现在得到相同的错误,我不知道如何解决这个问题。这是我到目前为止所得到的:

ContactController:

public ActionResult Edit(int id)
{
    Contact contact = db.Contacts
        .Include(c => c.Address)
        .Include(c => c.Relation)
        .Where(c => c.ContactId == id)
        .Single();
    return View(contact);
}


[HttpPost]
public ActionResult Edit(int id, FormCollection formCollection)
{
    var contactToUpdate = db.Contacts
        .Include(c => c.Address)
        .Include(c => c.Relation)
        .Where(c => c.ContactId == id)
        .Single();

    if (TryUpdateModel(contactToUpdate, "", null, new string[] { "Relation" }))
    {
        try
        {
            db.Entry(contactToUpdate).State = EntityState.Modified;
            db.SaveChanges();

            return RedirectToAction("Index");

        }
        catch (DataException ex)
        {
            ModelState.AddModelError("", "Error while saving.");
            return View();
        }
    }
    return View(contactToUpdate); 
}

我的模特的课程:

namespace MyApp.Models
{
    public class Contact
    {
        public int ContactId { get; set; }
        public string Weergavenaam { get; set; }

        public virtual ICollection<Address> Address{ get; set; }
        public virtual ICollection<Relation> Relation { get; set; }
    }

    public class Address
    {
        public int AddressId { get; set; }
        public string Street{ get; set; }
        public string Postal{ get; set; }
        public string Place{ get; set; }
        public string State{ get; set; }

        public DateTime Created { get; set; }
        public DateTime Modified { get; set; }

        public virtual Contact Contact { get; set; }
    }

    public class Relation
    {
        ...
    }
}

ContactController的编辑操作视图:

@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
    <legend>Contact</legend>

    @Html.HiddenFor(model => model.ContactId)

    <div class="editor-label">
        @Html.LabelFor(model => model.Name)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Name)
        @Html.ValidationMessageFor(model => model.Name)
    </div>

    @Html.EditorFor(model => model.Address) 


    <div class="display-field">
        Created: @Html.DisplayFor(model => model.Created)
    </div>
    <div class="display-field">
        Modified: @Html.DisplayFor(model => model.Modiied)
    </div>

    <p><input type="submit" value="Save" /></p>
</fieldset>
}

地址模型的EditorTemplate

@model MyApp.Models.Address
<fieldset>
    <legend>Address</legend>

    @Html.HiddenFor(model => model.AddressId)

    <div class="editor-label">
        @Html.LabelFor(model => model.Street)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Street)
        @Html.ValidationMessageFor(model => model.Street)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.Postal)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Postal)
        @Html.ValidationMessageFor(model => model.Postal)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.Place)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Place)
        @Html.ValidationMessageFor(model => model.Place)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.State)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.State)
        @Html.ValidationMessageFor(model => model.State)
    </div>
</fieldset>

1 个答案:

答案 0 :(得分:0)

在对代码进行了一些努力之后,我认为现在解决问题的唯一方法是使用@ Html.HiddenFor将缺少的属性添加到编辑器模板中。 当然,我确信应该有更优雅的方式让我的情况有效,但现在它解决了我的问题。

欢迎任何更好的解决方案!