How to Prevent ASP.NET MVC Action Method Ambiguity

It is sometimes easy to run into action method ambiguity when you have two action methods with the same name (e.g. one for POST and one for GET), like this:

[HttpGet]
public ActionResult NewBlogPost()
{
     return View();
}

[HttpPost]
public ActionResult NewBlogPost()
{
     BlogPost newBlogPost = new BlogPost();
     TryUpdateModel<INewBlogPost>(newBlogPost);
     if (ModelState.IsValid)
     {
         // ...
     }
}

In this example the compiler will complain that your two methods are ambiguous. An easy way to fix this is to modify your POST action method to look like this:

[HttpPost]
public ActionResult NewBlogPost(FormCollection formCollection)
{
     BlogPost newBlogPost = new BlogPost();
     TryUpdateModel<INewBlogPost>(newBlogPost, formCollection);
     if (ModelState.IsValid)
     {
         // ...
     }
}

The MVC model binder knows how to bind the request form collection to an argument of type FormCollection so it will populate this just fine. Because your POST action now accepts an argument, it is no longer ambiguous with your GET method. You can pass this formCollection into TryUpdateModel or UpdateModel to be used as the binding source if you wish, but you don't have to as it will default to the request form collection anyway. But since you are passing it in you may as well use it :)