0

I'm trying to switch views from a controller after checking the login info. How can I properly switch to the correct view after running this logic?

I want to switch to the question explanation view. My existing code is below:

[HttpPost]
public IActionResult LoginBody(LoginModel info)
{
    System.Diagnostics.Debug.WriteLine(info.Email);
    System.Diagnostics.Debug.WriteLine(info.Password);

    //Some login Verification Logic

   return View("~\\Views\\QuestionExplanation\\QuestionExplanation");        
}

My folders are organized as follows:

enter image description here

The code for the QuestionExplanationController is as follows:

namespace OliviaSite.Controllers
{
    public class QuestionExplanationController : Controller
    {
        // GET: /<controller>/
        [HttpGet]
        public IActionResult ShowView()
        {
            return View();
        }
    }
}
DMop
  • 463
  • 8
  • 23
  • 2
    `return RedirectToAction("QuestionExplaination","QuestionExplaination");` Where the first argument stands for the Action and the second for the Controller. If you want to pass an argument see [here](https://stackoverflow.com/a/1257632/4625144). – Dimitar Nikovski Jan 07 '18 at 19:16
  • @DimitarNikovski I get a blank page and when I inspect, I get a 404 error from that. Do you know why that might happen? – DMop Jan 07 '18 at 19:24
  • What methods are in your QuestionExplanation controller, and does it exist at all? – Adam Brown Jan 07 '18 at 19:43
  • @AdamBrown It exists in my controllers folder along with LoginController (where this logic is happening) The only method I have is "ShowView" which just returns the view – DMop Jan 07 '18 at 19:52
  • Please post the code of that controller. Thanks. – Adam Brown Jan 07 '18 at 20:29
  • @AdamBrown it's now above. Thank you – DMop Jan 07 '18 at 20:53

1 Answers1

1

As Dimitar Nikovski said, you can return RedirectToAction from login action to redirect into ShowView action method:

[HttpPost]
public IActionResult LoginBody(LoginModel info)
{
    System.Diagnostics.Debug.WriteLine(info.Email);
    System.Diagnostics.Debug.WriteLine(info.Password);

    //Some login Verification Logic

   // usage: RedirectToAction("action_name", "controller_name")
   return RedirectToAction("ShowView", "QuestionExplanation");        
}

Then, mention target view name in ShowView action method (you can use any different view name other than action name, ensure that view name exists in Views/QuestionExplanation):

// QuestionExplanationController
[HttpGet]
public IActionResult ShowView()
{
    return View("QuestionExplanation"); // mention view name here
}

The 404 error occurred because view engine tried to find view name supplied by LoginBody method which was incorrectly set.

NB: This path seems incorrect:

return View("~\\Views\\QuestionExplanation\\QuestionExplanation");

The correct relative path should be like this, based on similar issue:

return View("~/Views/QuestionExplanation/QuestionExplanation.cshtml");
Tetsuya Yamamoto
  • 24,297
  • 8
  • 39
  • 61