Do you have a post action that accepts a parameter (and what post action doesn’t?), but you also have a get action that accepts a parameter and you are seeing that submits are going back to the get action and other strange behavior? Here’s how you fix that:


If your get controller action takes a parameter and the runtime gets confused to which controller action to post to, instead of adding an ActionName attribute to your post action, just modify your global.asax.cs’ default route to set the id to UrlParameter.Optional, like this:

 
routes.MapRoute(
     "Default", // Route name
     "{controller}.mvc/{action}/{id}", // URL with parameters
         new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
     );

Example: if you have a controller action like this:

public ActionResult Edit(string id)
{
	...
}

You can still easily post model values to a post controller action like this:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(MyVM viewModel)
{
	...
}

Your view page can still have the standard Html.BeginForm() without specifying any controller action in the parameters:

<% using (Html.BeginForm()) { %> 
...
<% } %>

and

<%: Html.AntiForgeryToken() %>

« »