0

I've got a text box that upon initial page load (and assuming an empty model) the html TextBoxFor helper populates a text box with 0. Since 0 is a value, I want to change this behavior to "" (null, nothing, etc.). Given the amount of literature out there on this, I thought this should be straightforward; however, I can't get it to work. Here's what I've got (I've parsed it down to keep it simple, pardon any mistakes)

Model:

public class Something_ViewModel
{ [DisplayName("Feet:")]
    public int Feet{ get; set; }
}

Controller:

public IActionResult RetainedOwnership()
    {
        Models.Analytics.Something_ViewModel RO_VM = new Models.Analytics.Something_ViewModel();
       Return View(RO_VM);
    }

And the tag Helpers I've tried:

    @Html.TextBoxFor(Model => Model.Feet, new { id = "txtFeet", @Value = Model.Feet.ToString() ?? "" })
    @Html.TextBoxFor(Model => Model.Feet, new { id = "txtFeet", Value = Model.Feet.ToString() ?? "" })
    @Html.TextBoxFor(Model => Model.Feet, new { id = "txtFeet", Value = "" })
    @Html.TextBoxFor(Model => Model.Feet, new {Value = "" })
    @Html.TextBoxFor(Model => Model.Feet, new {@Value = "" })
    @Html.TextBoxFor(Model => Model.Feet, new {id = "txtFeet"})
1
  • Under no circumstances should you ever set the value attribute when using the HtmlHelper methods Commented Jan 27, 2018 at 22:26

1 Answer 1

1

You might want to change Feet datatype to int?. Default value for that would be null.

Sign up to request clarification or add additional context in comments.

1 Comment

That worked. For everyone else this is what I discovered- int, decimal, float, etc. value types are by default required and non-nullable. If you want it to be nullable, you have to do this:System.Nullable<int> or the shorthand for this is int? .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.