How to return ActionResult with specific View (not the controller name)

I have a method SendMail in the MVC Controller.This method calls other method ValidateLogin. This is the signature of the Validate Login:

private ActionResult ValidateLogin(Models.ResetPassword model)

When I call the ValidateLogin from SendMail, this exception appears because the controller try to search a view SendMail, but I want to load the ResetPassword View:
Global Error - The view 'SendMail' or its master was not found or no view engine supports the searched locations. The following locations were searched: ...

This is the code of the SendMail:
public ActionResult SendMail(string login)
{
        return ValidateLogin(login);
}

How Can I override the View on the return statement?

Thanks in advance

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

private ActionResult SendMail(string login)
{
            return View("~/Views/SpecificView.cshtml")
}

You can directly point towards specifc view by pointing to their location explicitly ..

Method 2

finally, this was the solution

return View("ResetPassword", new ResetPassword
            {
                fields= fields
            });

Method 3

The View method has a overload which get a string to a viewName. Sometimes you want to pass a string as a model and asp.net framework confuses it trying to find a view with the value string. Try something like this:

public ActionResult SendMail(string login)
{
   this.Model = login; // set the model
   return View("ValidateLogin"); // reponse the ValidateLogin view
}

Method 4

If SendMail was a POST, you should use the POST-REDIRECT-GET pattern

    public ActionResult SendMail(string login)
    {
        ...        
        return RedirectToAction("ResetPassword", login);
    }

    public ActionResult ResetPassword(string login)
    {
        ...
        return View("ResetPassword", login);
    }

This will protect you from a double-post in IE

Method 5

You can return view by a name like this

return View("viewnamehere");


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x