简体   繁体   English

为什么我不能在同一个控制器中调用另一个方法?

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

I try to call method that returns View inside of another method, but in the same controller. 我尝试调用在另一个方法中返回View的方法,但是在同一个控制器中。 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() 并且有一个调用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. 我也尝试过RedirectToAction()并从另一个控制器调用方法。
I would appreciate any help. 我将不胜感激任何帮助。

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从一个控制器到另一个控制器调用操作方法 - Call action method from one controller to another 如何在MVC C#中从另一个Action方法(都在同一个Controller中)调用一个Action方法? - How to call one Action method from another Action method(both are in the same Controller) in MVC C#? 我可以在另一个DataContext中调用另一个方法吗? - Can I call a Method in one DataContext from another? 如果我在调用中使用相同的URL,我的控制器如何知道何时在另一方法上执行一个方法(更新/插入)? - How does my controller know when to execute one method over another (update/insert) if I have the same URL on the call? 当从一个控制器调用一个方法到另一个控制器会话的方法变为空时 - when call one method from one controller to another controller session becomes null 如何从另一个控制器调用方法? - How do I call a method from another controller? C#-如何在同一个类的另一个方法中调用变量 - C# - How can I call a variable in a method from another in the same class 如何从MVC控制器动作调用asyc方法? - How can I call an asyc method from MVC controller action? 我无法从视图中调用控制器中的action方法 - I can't to call action method in the controller from view 为什么我不能在另一个类中调用公共方法? - Why can't I call a public method in another class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM