ASP.NET MVC Ajax 删除未映射不能为空
ASP.NET MVC Ajax Delete Notmapped Cannot be null
我在尝试删除值时遇到错误,具体过程如下。
我的应用程序有调查,它们与 SurveyQuestions 具有一对多关系,而 SurveyQuestions 与 SurveyAnswers 具有一对多关系。 CascadeOnDelete 完美运行,但是当我尝试使用 Ajax 时,它会发送一个错误
Value cannot be null.
Parameter name: 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
,但它里面没有任何项目,或者您可以调整 getter 以将其视为 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));
}
}
我在尝试删除值时遇到错误,具体过程如下。 我的应用程序有调查,它们与 SurveyQuestions 具有一对多关系,而 SurveyQuestions 与 SurveyAnswers 具有一对多关系。 CascadeOnDelete 完美运行,但是当我尝试使用 Ajax 时,它会发送一个错误
Value cannot be null.
Parameter name: 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
,但它里面没有任何项目,或者您可以调整 getter 以将其视为 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));
}
}