Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing Information Between Controllers in ASP.Net-MVC

Tags:

asp.net-mvc

This is a duplicate of How to RedirectToAction in ASP.NET MVC without losing request data


Hi, I have come into a problem which is making me scratch my head a little bit. Basically I have a login page Login.aspx , which has username and password fields, as well as an important little checkbox. The login is handled in the AccountController Login method. The code currently is as follows:

[AcceptVerbs(HttpVerbs.Post)]
[SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings",
    Justification = 
        "Needs to take same parameter type as Controller.Redirect()")]
public ActionResult LogOn(string userName, string password, string returnUrl, 
    bool sendStoredInfo)
{
    if (!this.ValidateLogOn(userName, password)) {
        return View();
    }

    this.FormsAuth.SignIn(userName, false);

    if (!String.IsNullOrEmpty(returnUrl)) {
        return Redirect(returnUrl);
    } else {
        return RedirectToAction("Index", "Home");
    }
}

Basically, if the line return Redirect(returnUrl); fires, then it will end up in another controller, the OpenIDController, and it is that situation where the sendStoredInfo bool becomes important. But the problem is I have no reference to it when I'm in the OpenIDController. How can I send this value across?

like image 428
Jacob Bellamy Avatar asked Feb 15 '10 22:02

Jacob Bellamy


People also ask

How do you pass data between action methods in MVC?

There are a number of ways in which you can pass parameters to action methods in ASP.NET Core MVC. You can pass them via a URL, a query string, a request header, a request body, or even a form.


1 Answers

Change the call to:

return RedirectToAction("LoginFailed", new { sendFlag = sendStoredInfo });

The controller action method signature could be something like:

public ActionResult LoginFailed(bool sendFlag)
{
    ...
}
like image 73
Neil T. Avatar answered Oct 15 '22 12:10

Neil T.