Reputation: 641
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
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