1

I need to Redirect to Action with a model object from another action method. It is ok but when i do this, i can see all the parameters in the URL address bar. Since it is about payment, it is not ok for me ?

I can do this basicly passing ID but my model is viewmodel and there is no Key.

How can i prevent this.

umki
  • 769
  • 13
  • 31
  • check this http://stackoverflow.com/questions/11209191/how-do-i-include-a-model-with-a-redirecttoaction/11209320#11209320 – Shyju Mar 13 '14 at 23:07

2 Answers2

2

In this scenario, what you really should be doing is return a view rather than redirect.

Like:

return View(viewModel);

But if you really prefer to do a redirect, you can place the ViewModel in the TempData, and then redirect to the action:

TempData["MyViewModelFromRedirect"] = viewModel;

And in your redirected action:

var ViewModel = (MyViewModel)TempData["MyViewModelFromRedirect"];
Ganesh
  • 245
  • 1
  • 9
  • Is it safe to use TempData, what if it happens, at same time two user click the the submit button on the post ? Any problem ? Thanks for your answer – umki Mar 13 '14 at 23:47
  • There won't be any issues with multiple users as its stored in the Session for each user. TempData is basically a Session storage. The only difference is sessions expire after a certain time, while TempData expires as soon as the redirected action is complete unless you specifically persist it using the TempData.Keep() method for one more request. – Ganesh Mar 14 '14 at 20:19
1

Redirect result will return the HTTP redirect result (302) to the browser, with the whole parameters in the url. If you're passing model properties in the routevalues, they will be serialised to strings.

So, as you said the browser (at client side) will see all those parameters, and the browser will issue another GET request to the new url.

The recommended approach in this case it to use TempData at the controller to set all your server-side data. Then redirect to the new action.

TempData["mymodel"] = myModel;
return Redirect(Url.Action("newaction", "newcontroller"));

And in the new Action, you can just retrieve your model from TempData

Emad
  • 544
  • 2
  • 6