1

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?

3 Answers 3

4

Try this - Make int nullable

public int? zip { get;set; }
Sign up to request clarification or add additional context in comments.

Comments

3

Alternatively, if you do not want to have to use nullables:

@Html.TextBoxFor(x => x.zip, new { placeholder = "Enter your zip", @class = "gsTextBox", @Value = (Model.zip > 0 ? Model.zip.ToString() : string.Empty) })

The empty string will cause the placeholder to appear. In the event of a validation error or similar and returning to the page, this code will show the value the user entered... or the placeholder if not greater than 0.

Comments

0

if you Wont change your Model by Nullable Property:

Use javascrip or Jquery Way.

$("input.no-init").val("");

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.