Why I can not call method from another one in the same controller?

别等时光非礼了梦想. 提交于 2019-12-11 04:27:12

问题


I try to call method that returns View inside of another method, but in the same controller. There is my code:

public ActionResult GetAnswer(List<PossibleAnswerVM> possibleAnswers)
{
    if (IsLastQuestion(possibleAnswers.FirstOrDefault().IdQuestion))
    {
        return Index();
    }
    return null;
}
[HttpGet]
public ActionResult Index()
{
    IEnumerable<Anketa> anketas = Manager.SelectAnketa();
    return View(anketas);
}

And there is the form which call GetAnswer()

@using (Ajax.BeginForm("GetAnswer", "Survey", FormMethod.Post,
    new AjaxOptions { InsertionMode = InsertionMode.Replace }))
{
    <input type="submit" value="Ответить" 
        onclick="answerClick(@Model.FirstOrDefault().OrdQuestion);" 
        class="btn btn-default" />
}

Also I have tried RedirectToAction() and call methods from another controller.
I would appreciate any help.


回答1:


Try this.

public ActionResult GetAnswer(List<PossibleAnswerVM> possibleAnswers)
{
    if (IsLastQuestion(possibleAnswers.FirstOrDefault().IdQuestion))
    {
          IEnumerable<Anketa> anketas = Manager.SelectAnketa();
          return View("VIEWNAME", anketas);
    }
    return null;
}

private static bool IsLastQuestion(int idQuestion)
{
    var returnValue = false;

    Question question = Manager.GetQuestion(idQuestion);
    List<Question> questions = Manager.SelectQuestions(question.idAnketa);

    if (questions.Count == Manager.GetCountQuestionsAnswered(question.idAnketa, SessionUser.PersonID))
    {
        returnValue = true;
    }                      
    return returnValue;
}


来源:https://stackoverflow.com/questions/37439006/why-i-can-not-call-method-from-another-one-in-the-same-controller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!