Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML Textbox showing default value as 0 for int data field

I have a textbox which is holds int data field. When the page loads, 0 appears by default. Following is the code for displaying text box :

<%:Html.TextBoxFor(model => model.Ssn, htmlAttributes: new { @class = "txtBox txtBoxMediumSmall" })%>

where model.Ssn is a int.
Why isn't is blank? And what should be done to make it blank?
Please let me know if more information is required.

like image 507
Nitish Avatar asked Sep 29 '12 06:09

Nitish


People also ask

What is default value of number?

For numeric types, the default is 0 , with the exception that for integer or floating-point types declared with the AUTO_INCREMENT attribute, the default is the next value in the sequence.


2 Answers

You can do this

public class MyModel
{
   //Nullable integer
   public int? Ssn {get; set;}  //this should be null by default

   public int SSN {get; set; } //this should be 0 by default

}
like image 133
codingbiz Avatar answered Nov 15 '22 09:11

codingbiz


As bittech poinetd out int is struct and does not have "empty" value - 0 is default, but perfectly valid value.

You can try to use int? (Nullable<int>) if you need to destinguish "no value" vs. "default value".

like image 29
Alexei Levenkov Avatar answered Nov 15 '22 09:11

Alexei Levenkov