Chris
Chris

Reputation: 289

Set default value in @Html.TextboxFor MVC Core 2

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"})

Upvotes: 0

Views: 899

Answers (1)

tearius
tearius

Reputation: 58

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

Upvotes: 1

Related Questions