MVC3 更新关联数据问题
Model中:
[Table("CompanyInfo")] public class CompanyInfo { [Key] public int id { get; set; } [Required(AllowEmptyStrings=false, ErrorMessage="请输入单位名称")] public string CompanyName { get; set; } [Required(AllowEmptyStrings=false, ErrorMessage="请选择省份")] public string ProvinceId { get; set; } [Required(AllowEmptyStrings = false, ErrorMessage = "请选择城市")] public string CityId { get; set; } public List<AccountInfo> AccountInfos{ get; set; } public List<DepartmentId> DepartmentIds { get; set; } }
[Table("AccountInfo")] public class AccountInfo { [Key] public int id { get; set; } [Column("CompanyId")] public int CompanyInfoId { get; set; } public string AccountName { get; set; } public string TaxNumber { get; set; } public string Address { get; set; } public string Telephone { get; set; } public string Account { get; set; } public string Bank { get; set; } }
[Table("DepartmentId")] public class DepartmentId { [Key] public int id { get; set; } [Column("CompanyId")] public int CompanyInfoId { get; set; } [Required(AllowEmptyStrings=false, ErrorMessage="请输入下属部门名称")] public string DepartmentName { get; set; } }
public ActionResult Edit(int id) { CompanyInfo companyInfo = CRMDB.CompanyInfos.Include(x => x.AccountInfos).Include(x => x.DepartmentIds).Where(x => x.id == id).Single(); return View(companyInfo); }[HttpPost] public ActionResult Edit(CompanyInfo companyInfo) { if (ModelState.IsValid) { CRMDB.Entry(companyInfo).State = EntityState.Modified; CRMDB.SaveChanges(); return RedirectToAction("Index"); } return View(companyInfo); }
<tr>
<td>
@Html.EditorFor(model => item.DepartmentName)
@Html.ValidationMessageFor(model => item.DepartmentName)
</td>
<td>
@Html.ActionLink("Delete", "Delete", "DepartmentIds", new { id = item.id })
</td>
</tr>
}
}
</table>
</fieldset>
<p>
<input type="submit" value="提交保存" />
</p>
//提交更新——却没有修改保存关联部分的信息
[解决办法]
companyInfo在更新的时候,含有需要更新的对象吗?他们的数据是脏的吗?