开发者

TextBoxFor in EditorTemplate or DisplayTemplate

开发者 https://www.devze.com 2023-04-03 21:55 出处:网络
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.

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 than TextBoxFor(). 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.


If you want to apply the style for all text input fields you can modify the css style that comes with the MVC template.

input[type="text"]
{
    border: 1px solid #ccc;
    padding: 2px;
    font-size: 1.2em;
    color: #444;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消