In a C# controller, I have a function defined with an optional parameter which is set to default to null (see code example below). The first time the page loads, the function is called and the filter is passed in as an initialized object, despite the default value being null. I would like it to be null the first time the page loads. Is there a way to do this?
public ActionResult MyControllerFunction(CustomFilterModel filter = null)
{
if (filter == null)
doSomething(); // We never make it inside this "if" statement.
// Do other things...
}
This action is resolved by the following route definition:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Project", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);