我正在尝试用新的信息更新我的视图,所以当它被发送到控制器保存时,信息就会被传递。同样的方法在创建对象(Create-View)时工作得很好,但会抛出ajax错误500:在编辑现有对象(Edit-View)时序列化'System.Data.Entity.DynamicProxies.Gutscheine_',类型的对象时,检测到循环引用。
下面是我的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);
}
})
}下面是方法:
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:
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);
}循环引用给出了一些提示,但我对这一切都很陌生,所以它对我找出问题没有多大帮助。如果你有任何想法如何解决这个问题,请让我知道。如有任何帮助,我们不胜感激!
发布于 2019-10-24 19:48:23
在从数据库获取对象之前添加db.Configuration.ProxyCreationEnabled = false;可以解决这个问题。
完成代码:(我还在最后删除了一些东西,这把其他东西搞砸了)
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);
}非常感谢提供此链接的评论者!
https://stackoverflow.com/questions/58535354
复制相似问题