asp.net-mvcasp.net-mvc-4html-helperhtml.textboxfor

Make Html.TextBoxFor show non-initialized integer property as empty string in MVC 4


I have added Html.TextBoxFor to my view:

@Html.TextBoxFor(x => x.zip, String.Empty, new { @class = "gsTextBox", placeholder = "Enter your zip" });

And it shows 0 in the input instead of empty string.

I also have found this solution, but it seems to be horrible. Or this is nice approach? Or maybe I should replace 0 by empty string using javascript?


Solution

  • Try this - Make int nullable

    public int? zip { get;set; }