Vivek
Vivek

Reputation: 641

how to make a property `Not Required` in MVC 3

I am new to asp.net MVC
some of the properties in My model especially property of type int become required by default without decorating it with [Required] attributes.however the problem is solved by making the filled nullable i.e int? Some property.
But i am not sure that this is the best approach or not... also i doubt if making a property nullable gonna make some problem later in code or not.
Any suggestion is highly appreciable.
Thanks in advance !!! :)

Upvotes: 3

Views: 3571

Answers (2)

Har
Har

Reputation: 5004

Nullable is the preferable approach. It's not sloppy.

Upvotes: 1

Derek Risling
Derek Risling

Reputation: 364

Nullable is okay, but you need to take extra care when using values that could be null. The easiest is to use the coalesce null operator ??

var foo = myNullableField ?? new bar()

If the nullable field is, in fact null, foo will be assigned as a new object (in this example, you could use a default string, int, whatever). If it's not null it'll take the value from myNullableField

Upvotes: 5

Related Questions