chester89
chester89

Reputation: 8617

If property or field is excluded when model-binding, what value will it have?

The question is in the title, actually - let's say I have a simple class like this:

public class Product {
   public Int32 ID { get; set; }
   public String Name { get; set; }
   //...
}

When I use it in action method, like this:

public ViewResult DoSomething([Bind(Exclude="ID")]Product product] {
 //...
}

what value will product.ID have inside this action method? Maybe it will be default value for Int32? And null in case ID is reference-type? I'm just interested, didn't find it on the web.

Upvotes: 0

Views: 181

Answers (2)

Thomas Weller
Thomas Weller

Reputation: 11717

Since there will be no initialization at all, the property will have its default value.

Upvotes: 1

bzlm
bzlm

Reputation: 9727

The DefaultModelBinder, which you are using if nothing else is specified, indeed uses default(T) for unbound values.

To change this, you can modify binding behavior per-parameter in action methods (as you are doing with the BindAttribute in your example), or per-type in eg. Global.asax.

Upvotes: 0

Related Questions