1

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.

Vitalii Isaenko
  • 941
  • 1
  • 14
  • 37

1 Answers1

0

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;
}
David
  • 641
  • 1
  • 8
  • 23
  • I tried to return `View()` instead of `Action()`, but it didn't help too. In addition, I need to initialise object for strongly-typed view. – Vitalii Isaenko May 25 '16 at 14:01
  • @VitalyIsaenko can you post the code in your Index() method, ive changed above to return the index() - nice to see whats in there. – David May 25 '16 at 14:02
  • yes, this approach helped, thank you. But why can't I return method instead of view? – Vitalii Isaenko May 25 '16 at 14:18