我在尝试删除值时遇到错误。我的应用程序有调查,他们与SurveyQuestions有一对多关系,与SurveyAnswers有一对多的关系。 CascadeOnDelete工作得很好但是当我尝试使用Ajax时它发送错误
值不能为空。 参数名称:source
但它完全删除了调查和相关的问题和答案,它只是以某种方式发送它是ajax的错误。
我的代码:
public JsonResult DeleteSurvey(int id)
{
SurveyViewModel survey = Mapper.Map<Survey, SurveyViewModel>(_unitOfWork.SurveyRepository.GetByID(id));
if (survey != null)
{
_unitOfWork.SurveyRepository.Delete(id);
_unitOfWork.Save();
}
return Json(survey, JsonRequestBehavior.AllowGet);
}
$.ajax({
type: "POST",
url: "/Survey/DeleteSurvey",
dataType: 'json',
data: {Id: surveyID},
success: function (response) {
$("#myModal").modal("hide");
alert("Survey Deleted");
location.reload();
},
error: function (response) {
alert("Error");
},
failure: function () {
alert("Failed to upload");
}
})
更新:
了解有关错误的更多信息:
<b> Exception Details: </b>System.ArgumentNullException: Value cannot be null.<br>Parameter name: source<br><br>
<b>Source Error:</b> <br><br>
<table width=100% bgcolor="#ffffcc">
<tr>
<td>
<code><pre>
Line 44: if (user != null)
Line 45: {
<font color=red>Line 46: return Questions.Any(m => m.Answers.Any(x => x.UserId == user));
</font>Line 47: }
Line 48: return false;</pre></code>
这是ViewModel中的那个部分
[NotMapped]
public bool CompletedByUser
{
get
{
var user = System.Web.HttpContext.Current.User.Identity.GetUserId();
return Questions.Any(m => m.Answers.Any(x => x.UserId == user));
}
}
那是因为Questions
在删除后即将到来null
,你可能需要它实例化,以便它不是null
但它没有任何物品或你可以调整你的吸气剂来考虑它的null
状态以及:
[NotMapped]
public bool CompletedByUser
{
get
{
var user = System.Web.HttpContext.Current.User.Identity.GetUserId();
return Questions == null ?
false :
Questions.Any(m => m.Answers!=null && m.Answers.Any(x => x.UserId == user));
}
}