0

我有一个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

4

2 回答 2

1

您必须将tablo实例附加到上下文。

[HttpPost]
public ActionResult EditTablo(Tablo tablo, int? RessamId, HttpPostedFileBase image)
{ 
    if (ModelState.IsValid)
    {
        container.Tablo.Attach(tablo);
        container.ObjectStateManager
            .ChangeObjectState(tablo, System.Data.EntityState.Modified);

        if (RessamId != null)
        {
            tablo.Ressam = (from table in container.Ressam
                            where table.RessamId == RessamId
                            select table).Single();

            TryUpdateModel(tablo.Ressam);
        }

        container.SaveChanges();
    }

    return View(tablo);
}
于 2012-07-14T02:44:53.447 回答
0

不用多说,这是完成这项工作的代码:

       [HttpPost]
        public ActionResult EditTablo(Tablo tablo, int? RessamId, HttpPostedFileBase image)
        {
            if (ModelState.IsValid)
            {
                if (tablo is TuvalBaski)
                {
                    container.Urun.Attach((TuvalBaski)tablo);
                }
                else if (tablo is YagliBoya)
                {
                    container.Urun.Attach((YagliBoya)tablo);
                }

                if (RessamId == null)
                {
                    if(tablo.Ressam != null)
                    {
                        container.Ressam.Detach(tablo.Ressam);
                    }

                    tablo.Ressam = null;
                }
                else
                {
                    if (tablo.Ressam != null)
                    {
                        container.Ressam.Detach(tablo.Ressam);
                    }

                    tablo.Ressam = (from table in container.Ressam
                                    where table.RessamId == RessamId
                                    select table).Single();


                    container.Ressam.Attach(tablo.Ressam);
                }

                TryUpdateModel(tablo);
                container.SaveChanges();
            }

            return View(tablo);
        }
于 2012-07-14T12:11:53.393 回答