HTML text field displaying default value as 0 for int data field

I have a text field that contains an int data field. When the page loads, the default is 0. The following is the code to display the text field:

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

where model.Ssn is int .
Why isn’t empty? And what needs to be done to make it empty?
Please let me know if further information is required.

+4
source share
3 answers

You can do it

 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 } 
+7
source

Since bittech poinetd out int is a structure and does not have an "empty" value - 0 is the default value, but it is a valid value.

Can you try using int? ( Nullable<int> ) if you need to specify "no value" and "default value".

+2
source

I will do like this:

 @Html.TextBoxFor(model => model.Ssn, new { @Value = (@Model.Ssn == 0) ? "" : @Model.Ssn.ToString()}) 
0
source

Source: https://habr.com/ru/post/1436928/


All Articles