Passing Information Between Controllers in ASP.Net-MVC

后端 未结 4 907
北恋
北恋 2020-12-09 22:56

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 l

相关标签:
4条回答
  • 2020-12-09 23:16

    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;
    }
    
    0 讨论(0)
  • 2020-12-09 23:16

    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

    0 讨论(0)
  • 2020-12-09 23:21

    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

    0 讨论(0)
  • 2020-12-09 23:27

    Change the call to:

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

    The controller action method signature could be something like:

    public ActionResult LoginFailed(bool sendFlag)
    {
        ...
    }
    
    0 讨论(0)
提交回复
热议问题