In our project we are using TextBoxFor in many places.
We would like to set the height and width of the TextBoxFor wherever we have used, for that we have done this.
<%= Html.TextBoxFor(x => x, new { @class = "TextBoxFor" } ) %>
.TextBoxFor
{
height:30px;
width:250px;
font-family:@Batang;
font-size:20px;
font-weight: bold;
}
Is it possible to to put the <%= Html.TextBoxFor("", new { @class = "TextBoxFor" } ) %> somewhere in a EditorTemplate / DisplayTemplate and in other places we just call like this
<%= Html.TextBoxFor(x => x) %>
Which means we are not specifying the css class name. So it is easy to maintain.
Any ideas. Thanks
Update, I created a static helper but it is having errors..
public static MvcHtmlString CustomTextBoxFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, object htmlAttributes)
{
return htmlHelper.CustomTextBoxFor<TModel, TProperty>(expression, new { @class = "TextBoxFor" });
}
<%: Html.CustomTextBoxFor(m => m.Name) %>
Any ideas?
Yes, but you can also create your own Html helper, then do Html.MyTextBoxFor(…).
Check here for examples
You can download the MVC3Futures file, and it includes Templatized versions of the default templates, which you can put in your folder. You can edit these to customize the TextBoxFor used. However, you must use
@Html.EditorFor(...)in your view rather thanTextBoxFor(). Be aware, this will affect all uses of EditorFor in the scope where the templates are installed.By the way, if you’re using MVC3, I strongly suggest using the Razor engine rather than the WinForms engine. It’s less typing, and it’s more efficient, and it has features you’re not going to get in WinForms.. and it discourages WinForms thinking.