首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Ajax错误500:检测到循环引用

Ajax错误500:检测到循环引用
EN

Stack Overflow用户
提问于 2019-10-24 14:27:34
回答 1查看 103关注 0票数 0

我正在尝试用新的信息更新我的视图,所以当它被发送到控制器保存时,信息就会被传递。同样的方法在创建对象(Create-View)时工作得很好,但会抛出ajax错误500:在编辑现有对象(Edit-View)时序列化'System.Data.Entity.DynamicProxies.Gutscheine_',类型的对象时,检测到循环引用。

下面是我的ajax-call:

代码语言:javascript
复制
function onCloseButtonClick(s) {
        $.ajax({
            type: "POST",
            url: "@Url.Action("UpdateGutscheinEdit")",
            data: { sBild: document.getElementById(s).value, bIcon: true },
            success: function (response) {
                document.getElementById("sBild").value = response.sBild;
                document.getElementById("bIcon").value = response.bIcon;
                document.getElementById("fileToUpload").value = "";
                popupIconAuswaehlen.Hide();
            },
            error: function (jqxhr, status, exception) {
                alert(jqxhr.status); //throws 500
                alert('Exception', exception);
            }
        })
    }

下面是方法:

代码语言:javascript
复制
public ActionResult UpdateGutscheinEdit(string sBild, bool bIcon)
        {
            Gutscheine currentGutschein = Session["CurrentGutscheinEdit"] as Gutscheine;
            if (!string.IsNullOrEmpty(sBild))
            {
                currentGutschein.sBild = sBild;
                currentGutschein.bIcon = bIcon;
            }
            Session["CurrentGutscheinEdit"] = currentGutschein;
            return Json(currentGutschein);
        }

编辑(Get)-method是一个标准的a:

代码语言:javascript
复制
public ActionResult Edit(string id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Gutscheine gutscheine = db.Gutscheine.Find(id);
            if (gutscheine == null)
            {
                return HttpNotFound();
            }

            if(Session["CurrentGutscheinEdit"] == null)
            {
                Session["CurrentGutscheinEdit"] = gutscheine;
            }
            return View(Session["CurrentGutscheinEdit"] as Gutscheine);
        }

循环引用给出了一些提示,但我对这一切都很陌生,所以它对我找出问题没有多大帮助。如果你有任何想法如何解决这个问题,请让我知道。如有任何帮助,我们不胜感激!

EN

回答 1

Stack Overflow用户

发布于 2019-10-24 19:48:23

在从数据库获取对象之前添加db.Configuration.ProxyCreationEnabled = false;可以解决这个问题。

完成代码:(我还在最后删除了一些东西,这把其他东西搞砸了)

代码语言:javascript
复制
public ActionResult Edit(string id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            db.Configuration.ProxyCreationEnabled = false;
            Gutscheine gutscheine = db.Gutscheine.Find(id);
            if (gutscheine == null)
            {
                return HttpNotFound();
            }

            Session["CurrentGutscheinEdit"] = gutscheine;

            return View(Session["CurrentGutscheinEdit"] as Gutscheine);
        }

非常感谢提供此链接的评论者!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58535354

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档