SandSnark
SandSnark

Reputation: 90

Remove default 0 from numeric textbox

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

Answers (2)

user571874
user571874

Reputation: 589

Change model property type to nullable: public int? RackNumber {get;set;}

Upvotes: 5

Chris Bayles
Chris Bayles

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

Related Questions