GenesysStack.com

Did You Know that you can clear MVC TextboxFor default values?

If you like strongly-typed Views, but do not like showing default values that the user did not enter, such as -1 for integers, 0.0 for decimals, 01/01/1900 for default dates …you have to filter out the default values.

The trick is the Value Html attribute. Not “value”, “Value”.

To preserve values set by the user, but present empty string for default values, use an in-line if in the Value Html attribute, for example:

@Html.TextBoxFor(m => m.BirthDate, "{0:MM/dd/yyyy}", new { @id = "txtBirthDate", @class = "form-control BirthDate", @Value = Model.BirthDate == "01/01/1900" ? "" : Model.BirthDate.ToString(), @placeholder = "MM/DD/YYYY" })

Leave your comment
Comments
12/24/2016 9:04 AM
God help me, I put aside a whole afenotron to figure this out.