0
我试图让我的头绕着相关的数据,用于在MVC中使用EF的主细节场景。搜索StackOverflow让我走上了一条好路,但我在保存数据时遇到了问题。作为更多的示例StackOverflow我使用人/地址设置,以及人和地址(这是一对多)我只使用我的意见的一部分实体属性。使用默认脚手架会导致错误,但对于使用下面的代码设法解决的人员实体。但对于地址实体,我现在得到相同的错误,我不知道如何解决这个问题。以下是我到目前为止有:MVC/EF部分更新相关数据
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>
}
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数据类型转换为日期时间数据类型导致超出范围值。语句已终止。 – 2012-07-13 14:27:31
也试图改变:if(TryUpdateModel(contactToUpdate,“”,null,new string [] {“Relation”}))into:if(TryUpdateModel(contactToUpdate,“”,new string [] {“Address”},new string [] {“Relation”})) – 2012-07-13 14:29:36