【问题标题】:MVC/EF Partly updating Related DataMVC/EF 部分更新相关数据
【发布时间】:2012-07-13 01:21:17
【问题描述】:

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

联系人控制器:

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 的 Edit 操作的视图:

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

Address 模型的 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>

【问题讨论】:

  • 为了更完整。尝试保存时出现以下异常:将 datetime2 数据类型转换为 datetime 数据类型导致值超出范围。语句已终止。
  • 也尝试将:if (TryUpdateModel(contactToUpdate, "", null, new string[] { "Relation" })) 改成: if (TryUpdateModel(contactToUpdate, "", new string[] { "地址" }, 新字符串[] { "关系" }))

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


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2017-01-29
    • 1970-01-01
    • 2019-04-03
    • 1970-01-01
    • 1970-01-01
    • 2020-06-23
    • 2020-09-04
    • 2020-07-03
    • 1970-01-01
    相关资源
    最近更新 更多