Reputation: 5537
I have a method in a controller:
public ActionResult SendNotification(Guid? id=null, SendNotification notification =null)
It responds to /Apps/SendNotification/{guid}?{SendNotification properties}
SendNotification is a model with multiple string properties.
My issue is that SendNotification is NEVER null. No matter how I call the action .NET seems to always instantiate an object of SendNotification (with all fields null).
I even tested with the System.Web.Http.FromUri
and System.Web.Http.FromBody
and it still does that.
Any ideas?
Note: this is not WebApi. This is regular MVC.
My project only have the default route:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Upvotes: 6
Views: 208
Reputation: 4887
That is Model Binding tricking you.
Since it doesn't find any Parameter to bind to the Model, all properties are null but the model is still instantiated by default.
Instead of checking for the Model to be null, check for one of your main property (such as ID) to be null.
Upvotes: 2