What is use of TextBoxFor in ASP.Net MVC Razor

Last Reply one month ago By dharmendr

Posted one month ago

hi sir,

i want to know why this code is written in mvc in asp.net

<div class="col-md-2">
<label>Duration in Min</label>
    @Html.TextBoxFor(m => m.DurationInMinutes, new { id = "txtduration", @class = "form-control txtOnlyNumber", maxlength = "30" })
</div>

@Html.TextBoxFor(m => m.DurationInMinutes, why this is used

Posted one month ago Modified on one month ago

Hi sidu,

TextBoxFor helper method is a strongly typed extension method. It generates a text input element for the model property specified using a lambda expression.

TextBoxFor method binds a specified model object property to input text. So it automatically displays a value of the model property in a textbox and visa-versa.

Ex:

@Html.TextBoxFor(m => m.DurationInMinutes, new { id = "txtduration", @class = "form-control txtOnlyNumber", maxlength = "30" })

In the above example, the first parameter in TextBoxFor() method is a lambda expression which specifies DurationInMinutes property to bind with the textbox.

2nd parameter is htmlAttributes defines space for assign id, class etc.

For more details refer below article.

ASP.Net MVC: Html.TextBoxFor and Html.DropDownListFor example