Reputation: 4187
I've seen a lot of posts with Url.Action
passing null instead of the value, but none with my issue. I have one link that i want to be able to call a method with a null value.
The method is
public ActionResult ImpersonatePost(Role Role)
{
Authentication.Impersonating = Role;
return RedirectToAction("Index", "Home");
}
in the controller Impersonate
.
I have tried the following but they all end up with Role
being equal to new Role()
instead of null
. Role
is a class
.
<a href="@Url.Action("ImpersonatePost", "Impersonate", null)">None</a>
<a href="@Url.Action("ImpersonatePost", "Impersonate", default(Namespace.Role))">None</a>
Per Request Role is defined as (in format not actual properties
public class Role
{
String Property {get;set;}
}
Upvotes: 0
Views: 250
Reputation: 872
The problem is with the ASP.Net MVC3 default optional parameter binder. There is no proper way to make an optional url parameter bind to null.
If you really want to allow for a null parameter, I'd suggest adding it to your routing initialization code in the global.asax file.
routes.MapRoute(
"Custom",
"{controller}/{action}/{nullableParamName}",
);
If you have a method expecting nullableParamName in its route and it doesn't see that, it will take the nullableParamName in as null.
Upvotes: 1