[英]An exception of type 'System.NullReferenceException' : Object reference not set to an instance of an object
So I am really confused. 所以我真的很困惑。 I have some code:
我有一些代码:
public ActionResult submitSurveyQuestion(SurveyQuestion model)
{
SurveyQuestion nextQuestion = new SurveyQuestion();
nextQuestion = submitSurveyQuestionAndGetNextQuestionFromQuestion(model);
return RedirectToAction("generateViewForSurveyQuestion", new { question = nextQuestion });
}
public SurveyQuestion submitSurveyQuestionAndGetNextQuestionFromQuestion(SurveyQuestion currentQuestion)
{
SurveyQuestion surveyQuestion = new SurveyQuestion();
surveyQuestion.Question = "question";
//...etc, this just sets all the question properties
return surveyQuestion;
}
public ActionResult generateViewForSurveyQuestion(SurveyQuestion question)
{
//ERROR BELOW THIS LINE
return View("SurveyQuestionType" + question.QuestionType, question);
//ERROR ABOVE THIS LINE
}
But for some reason, my code returns the error: An exception of type 'System.NullReferenceException' : Object reference not set to an instance of an object.
但是由于某种原因,我的代码返回了错误:
An exception of type 'System.NullReferenceException' : Object reference not set to an instance of an object.
When looking through the debugger, it says that question = null
, but I set all the properties of question
and instantiate it, so I am really confused as to what's going wrong here.....any guidance would be greatly appreciated. 当通过调试器查看时,它说那个
question = null
,但是我设置了question
所有属性并实例化了它,所以我对这里出了什么question
感到很困惑.....任何指导将不胜感激。
You should call generateViewForSurveyQuestion()
directly to return the view: 您应该直接调用
generateViewForSurveyQuestion()
以返回视图:
public ActionResult submitSurveyQuestion(SurveyQuestion model) {
SurveyQuestion nextQuestion = new SurveyQuestion();
nextQuestion = submitSurveyQuestionAndGetNextQuestionFromQuestion(model);
return generateViewForSurveyQuestion(nextQuestion);
}
The overload of RedirectToAction()
which you are invoking requires route parameters , which your SurveyQuestion
object cannot properly represent. 您正在调用的
RedirectToAction()
的重载需要route参数 , SurveyQuestion
对象无法正确表示这些参数 。
I think you can use TempData as below. 我认为您可以使用TempData如下。
public ActionResult submitSurveyQuestion(SurveyQuestion model)
{
SurveyQuestion nextQuestion = new SurveyQuestion();
nextQuestion = submitSurveyQuestionAndGetNextQuestionFromQuestion(model);
TempData["question"] = nextQuestion;
return RedirectToAction("generateViewForSurveyQuestion");
}
public SurveyQuestion submitSurveyQuestionAndGetNextQuestionFromQuestion(SurveyQuestion currentQuestion)
{
SurveyQuestion surveyQuestion = new SurveyQuestion();
surveyQuestion.Question = "question";
//...etc, this just sets all the question properties
return surveyQuestion;
}
public ActionResult generateViewForSurveyQuestion()
{
// TempDate[question] available here......
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.