King_Fisher
King_Fisher

Reputation: 1213

Condional binding for Html.Textbox In MVC 5

How do i bind the data for html.textbox based on some condition in view like below.

If long_variable is 0, i want to assign empty to the Html.TextBox, else the value which is in long_variable.

Html.TextBox("long_variable", "", new { @class = "short"}

Upvotes: 0

Views: 38

Answers (1)

Shyju
Shyju

Reputation: 218922

You can check the value of this property and set the value of textbox conditionally.

If it is a local variable in your view,

@{
   long myLong = 0;

   @Html.TextBox("long_variable", myLong != 0 ? myLong.ToString() : "",
                                                                    new {@class = "short"});
}

If it is a property of your view model,

@Html.TextBox("long_variable",Model.MyLong != 0 ? Model.MyLong.ToString() : "",
                                                                    new {@class = "short"})

Upvotes: 1

Related Questions