Passing a model into RedirectToAction()

久未见 提交于 2019-12-12 10:53:44

问题


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?


回答1:


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.




回答2:


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.




回答3:


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




回答4:


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


来源:https://stackoverflow.com/questions/13957041/passing-a-model-into-redirecttoaction

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!