我试图了解相关数据,以便在 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>
}
地址模型的 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>