Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Html.TextBoxFor format and css class

The two lines of code below work fine, but I want to combine them. What I mean is: I want to use the @class in the first code line. How can I do that?

<%: Html.TextBoxFor(model => model.Product.Price, String.Format("{0:f}", Model.Product.Price))%>

<%: Html.TextBoxFor(model => model.Product.Name, new { @class = "textBox150" })%>

thanks,

Filip

like image 925
Filip Avatar asked Jan 22 '11 17:01

Filip


People also ask

What is HTML TextBoxFor?

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.

What is difference between HTML textbox and HTML TextBoxFor?

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 HTML EditorFor in MVC?

ASP.NET MVC includes the method that generates HTML input elements based on the datatype. The Html. Editor() or Html. EditorFor() extension methods generate HTML elements based on the data type of the model object's property.


1 Answers

I know I'm way late here but I just found a solution to this issue:

<%: Html.TextBoxFor(model => model.StartDate, new { @class = "datepicker", Value=String.Format("{0:d}", Model.StartDate) })%> 
like image 149
Cheddar Avatar answered Sep 21 '22 01:09

Cheddar