0

I'm trying to update my view with new information, so when it is sent to the controller for saving, the information gets passed. The same method works fine when creating an object (Create-View) but throws an ajax error 500: A circular reference was detected while serializing an object of type 'System.Data.Entity.DynamicProxies.Gutscheine_', when editing an existing object (Edit-View).

Here's my ajax-call:

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);
            }
        })
    }

And here's the method:

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);
        }

The Edit-(get)-method is a standard one:

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);
        }

Circular reference gives some hint, but I'm pretty new to all this, so it doesn't help me much in figuring out the problem. If you have any ideas how to fix this, please let me know. Any help is appreciated!

1 Answers1

0

Adding db.Configuration.ProxyCreationEnabled = false; before getting the object from the database did the trick.

Finished code: (I also removed some stuff at the end which screwed something else up)

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);
        }

Big thanks to the commenter that provided the link!