Question

I'm curious how this works. In MVC you can call View() and pass a model as a parameter, but RedirectToAction (one of its incarnations at least) takes a 'routeValues' object, which appears to be the closest match.

If your model is passed in this parameter will that model type be available in the subsequent action method? Or are there caveats involved that might prevent accurate translation in some circumstances?

Was it helpful?

Solution

If you need to pass in some-what complex objects to an action after a redirect, you probably want to use either a Session or TempData:

From "What is ASP.NET MVC TempData"

ASP.NET MVC TempData dictionary is used to share data between controller actions. The value of TempData persists until it is read or until the current user’s session times out

By default TempData uses a Session to persist the information, however, as with much of MVC, this is an extensibility point, where you can plug in a Cookie-based provider if you prefer.

OTHER TIPS

You cannot pass a model object in there but you can pass individual properties that will map to a model in the action that you redirect to.

That works by building up the url to redirect to using the properties, and the model binder in the receiving action.

Redirect... methods cause client-side-and-back trip, so - no, the model will not be available.

I think this is what you want :

  1. Save your model in a Tempdata

            RequestModel rq = new  RequestModel()   
            ....assign something to your model..
            TempData["request"] = rq;
    
            return Redirect("RequestAcknowledgement");
    
  2. Now create an Action Result for the view you are redirecting to and pass your TempData back to a model. Then return the model to a view.

     public ActionResult RequestAcknowledgement()
        {
           RequestsModel request =  (RequestsModel)TempData["request"];
    
           return View(request);
        }
    
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top