Jacob Bellamy
Jacob Bellamy

Reputation: 779

Passing Information Between Controllers in 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?

Upvotes: 6

Views: 41135

Answers (4)

Philip Wardlaw
Philip Wardlaw

Reputation: 199

Also consider using TempData to pass data from controller to controller. This can be advantageous as you wont have to expose the bool sendFlag interface potentially to the user.

Code in the first controller:

TempData["sendFlag"] = sendStoredInfo;
return RedirectToAction("LoginFailed");

Code in the second controller:

public ActionResult LoginFailed()
{
   bool sendFlag = TempData.ContainsKey("sendFlag")? TempData["sendFlag"]: false;
}

Upvotes: 14

Ali
Ali

Reputation: 927

As far as my knowledge serves me well, four different methods exist to handle passing data between controllers in asp.net MVC. They are 1. ViewData 2. ViewBag 3. TempData and 4. Sessions. If you may like a relatively good explanation besides a downloadable sample, please take a look at here

Upvotes: 1

casperOne
casperOne

Reputation: 74560

Because of the nature of redirects, you can only perform a GET operation.

This means that you have to pass the parameter as part of the query string.

So you would redirect to a url like http://host/dir/page?sendStoredInfo=true

Then, you can chose to have it part of your method signature in the other controller, or, you can choose to access it directly using the HttpRequest exposed by the HttpContext for the operation.

You can also call the RedirectToAction, as per this previous question:

How to RedirectToAction in ASP.NET MVC without losing request data

Upvotes: 1

Neil T.
Neil T.

Reputation: 3320

Change the call to:

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

The controller action method signature could be something like:

public ActionResult LoginFailed(bool sendFlag)
{
    ...
}

Upvotes: 14

Related Questions