我有一个Tablo
对象,它有一个对象的引用Ressam
。在我的编辑操作中Tablo
,我也希望能够更改Ressam
引用,即引用另一个RessamId
. 这是控制器代码,假设我只想更改调用中Ressam
的:Tablo
[HttpPost]
public ActionResult EditTablo(Tablo tablo, int? RessamId, HttpPostedFileBase image)
{
// Here, I successfully get RessamId, no problem there
if (ModelState.IsValid)
{
// this is where I attach the Tablo object
if (tablo is TuvalBaski)
{
container.Urun.Attach((TuvalBaski)tablo);
}
else if (tablo is YagliBoya)
{
container.Urun.Attach((YagliBoya)tablo);
}
// and this is the part where I change the Ressam reference
if (RessamId == null)
{
tablo.Ressam = null;
container.Ressam.Attach(tablo.Ressam);
TryUpdateModel(tablo.Ressam);
}
else
{
tablo.Ressam = (from table in container.Ressam
where table.RessamId == RessamId
select table).Single();
//container.Ressam.Context.ObjectStateManager.ChangeObjectState(tablo.Ressam, System.Data.EntityState.Modified);
//container.ObjectStateManager.ChangeObjectState(tablo.Ressam, System.Data.EntityState.Modified);
container.Ressam.Attach(tablo.Ressam);
TryUpdateModel(tablo.Ressam);
}
return View(tablo);
}
顺便说一句,这行不通。如何更新我的Tablo
实体中的参考 ID,以便它可以显示另一个Ressam
?