6

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?

4 Answers 4

14

Change the call to:

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

The controller action method signature could be something like:

public ActionResult LoginFailed(bool sendFlag)
{
    ...
}
Sign up to request clarification or add additional context in comments.

Comments

14

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

2 Comments

Thanks Jace Rhea, I've made the suggested changes.
why this bool sendFlag = TempData.ContainsKey("sendFlag")? TempData["sendFlag"]: false; will throw an exception saying "object null reference"?
1

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

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.