Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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"})
like image 650
SandSnark Avatar asked Mar 26 '13 19:03

SandSnark


2 Answers

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

like image 179
user571874 Avatar answered Sep 30 '22 23:09

user571874


You can provide the Value attribute like this:

@Html.EditorFor(model => model.RackNumber, new { Value = Model.RackNumber == 0 ? "" : Model.RackNumber.ToString(), id = "RackNumber"})
like image 29
Chris Bayles Avatar answered Sep 30 '22 23:09

Chris Bayles