MVC - Passing Data with RedirectToAction()

前端 未结 8 2119
既然无缘
既然无缘 2020-12-01 15:17

I\'d like to take data entered in an MVC user form and display it in a different view.

The class has the following private variable:

IList

        
8条回答
  •  日久生厌
    2020-12-01 15:59

    This is not working because RedirectToAction is actually sending back a Http 302 to the browser. When the browser receives this 302, it does a new request to the server asking for the new page. New request, new temp variables.

    You will also face this problem when you try to save/edit/delete something and for some reason you deny it and you have to return the old form again.

    So, instead of:

    return RedirectToAction("Preview", _pagecontent);
    

    Put the Preview logic in a separate method and just call it:

    return PreviewLogic(_pagecontent);
    

    You can also use the TempData[] dic to persist data for the next request like others have said, but then you will not avoid the 302 additional round trip to the server.

提交回复
热议问题