Reputation: 90
My model has an EditorFor that binds to a not null numeric field in a database. I wish to keep this field blank so that users can enter or scan numbers into the field. Unfortunately, it defaults to a 0. Is there an easy way to remove the 0 while keeping the field not null?
@Html.EditorFor(model => model.RackNumber, new { id = "RackNumber"})
Upvotes: 5
Views: 4663
Reputation: 589
Change model property type to nullable: public int? RackNumber {get;set;}
Upvotes: 5
Reputation: 405
You can provide the Value attribute like this:
@Html.EditorFor(model => model.RackNumber, new { Value = Model.RackNumber == 0 ? "" : Model.RackNumber.ToString(), id = "RackNumber"})
Upvotes: 4