Wednesday, May 2, 2012

Retaining route details on MVC Form Postback

I have a basic MVC form which is accessed through a GET Action with 3 string parameters pulled from the route.



[HttpGet]
public ActionResult Application(string x, string y, string z)
{
//create model, setup form, etc...

return View(model);
}


The route to access this form is configured as follows:



routes.MapRoute("Application", 
"application/{x}/{y}/{z}",
new
{
controller = "Application",
action = "Application",
x = "",
y = "",
z = ""
});


And the form is configured as follows:



Html.BeginForm("Application", "Application", FormMethod.Post)


All of this works until I click submit on the resulting form. From a routing perspective the correct POST Action is called and the Model is bound correctly. The problem is I have lost all the x/y/z route information which I still need. How can I preserve the route information?



I've tried a couple things:




  • Added route details to the Form in Hidden fields which are added to the form content correctly but never get returned in the Model on postback

  • Tried using the RouteValueDictionary overload for Html.BeginForm but can't figure out how to make it work. I may just need a proper example of how to use it and how to access the state from the Controller





No comments:

Post a Comment