Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Html.TextBoxFor with class and custom property (MVC)

How can translate that line using TextBoxFor (MVC):

<input id="Name" name="Name" type="text" data-bind="value: Name" class="title width-7" /> 

Thanks

like image 376
Paul Avatar asked Mar 25 '11 17:03

Paul


People also ask

How do I set the MaxLength for HTML TextBoxFor in MVC?

The TextBox for the Name value is created using Html. TextBoxFor function while the TextBox for the Mobile Number value is created using Html. TextBox helper function. The MaxLength of both the TextBoxes is set using the HTML MaxLength attribute using the HtmlAttributes parameter in Html.

What is the difference between HTML TextBox and HTML TextBoxFor using ASP NET MVC Razor engine?

IMO the main difference is that Textbox is not strongly typed. TextboxFor take a lambda as a parameter that tell the helper the with element of the model to use in a typed view. You can do the same things with both, but you should use typed views and TextboxFor when possible.

What is TextBoxFor in MVC?

TextBoxFor<TModel,TProperty>(HtmlHelper<TModel>, Expression<Func<TModel,TProperty>>, Object) Returns a text input element for each property in the object that is represented by the specified expression, using the specified HTML attributes.


2 Answers

MVC 3 will translate underscores in html attribute names into hyphens, so something like this should do the trick

@Html.TextBoxFor(m => m.Name, new { data_bind="value: Name", @class = "title width-7" }) 
like image 68
Lukáš Novotný Avatar answered Sep 20 '22 12:09

Lukáš Novotný


For example,

if you want to add data-mask

@Html.TextBoxFor(m => m.BirthDate, new { @class = "form-control", data_mask = "date" }) 

it will generate in html

data-mask="date" 
like image 26
hasanaydogar Avatar answered Sep 20 '22 12:09

hasanaydogar