Adding same classes to all html.textbox on asp.net mvc -


I have a project in ASP.NET MVC 4 that has recently been improved visually in the use of bootstrap .

Although I am very pleased with it, I feel that I am not very satisfied.

I have to add new {@class = "form-control input-sm"}

all to my html .TextBox was a helpful call and I was wondering if this is a very beautiful way of doing it ... maybe overriding the default textbox assistant?

Any insight into this will be highly appreciated.

edit

this is actually possible - To override the extension method. See for reference:.

The views "ASP" are created in the name space; Therefore, to override the default extension methods in System.Web.Mvc.Html (i.e. Static Class Input Input) for your extension method, you must declare your override in the "ASP" namespace, therefore, in your MVC project such class InputExtensionOverride.cs define:

  using the system; Using System.Collections.Generic; Using System.Linq.Expressions; Using System.Web.Mvc; Namespace ASP {Public static class input input overhead {public static MvcHtmlString TextBoxFor & lt; TModeel, TProperty & gt; (This HtmlHelper & lt; TModeel & gt; HTMLHelper, expression & lt; Func & lt; TModeel, TProperty & gt; & gt; Expression) {return InputExtensionsOverride.TextBoxFor & lt; TModeel, TProperty & gt; (htmlHelper, expression, (string) empty); } Public static MvcHtmlString TextBoxFor & lt; TModeel, TProperty & gt; (This HtmlHelper & lt; TModeel & gt; HTMLHelper, expression & lt; Func & lt; TModeel, TProperty & gt; Expression, IDictionary & lt; string, object & gt; html properties) {return InputExtensionsOverride.TextBoxFor & lt; TModeel, TProperty & gt; (HtmlHelper, expression, (string), html properties); } Public static MvcHtmlString TextBoxFor & lt; TModeel, TProperty & gt; (This HtmlHelper & lt; TModeel & gt; HTMLHelper, Expression & lt; Funky & amp; ;; TModeel; TProperty & gt; Expression, Object HTML Properties) Return InputExtensionsOverride.TextBox & lt; TModel, TProperty & gt; (html hiiler, expression, (string) tap, dictionary); } Public static MvcHtmlString TextBoxFor & lt; TModeel, TProperty & gt; (This HtmlHelper & lt; TModeel & gt; HTMLHelper, Expression & lt; Funk & lt; TModeel, TProperty & gt; Expression; String Format) {return InputExtensionsOverride.TextBoxFl & gt; TModeel, TProperty & gt; (HtmlHelper, Expression, Format, (id Works & lt; String, Object & gt;))); } Public static MvcHtmlString TextBoxFor & lt; TModeel, TProperty & gt; (This HtmlHelper & lt; TModeel & gt; htmlHelper, expression & lt; Func & lt; TModeel, TProperty & gt; Expression, string format, IDictionary & lt; string, object & gt; html properties) {html properties = setcamon properties & Lt; TModeel, TProperty & gt; (Html ​​html, expression, ref html property); Return system Web.Mvc.Html.InputExtensions.TextBoxFor (html bustling, expression, format, html properties); } Private Static IDictionary & lt; String, Object & gt; SetCommonAttributes & lt; TModel, TProperty & gt; (HtmlHelper & lt; TModeel & gt; HTMLHelper, expression & lt; Func & lt; TModeel, TProperty & gt; Expression; ref IDictionary & lt; string, object & gt; html properties) {if (htmlHelper == null) { New ArgumentNullException ("htmlHelper"); } If (expression == faucet) {new argument NullException ("expression"); } If (html property == tap) {html properties = new dictionary & lt; String, Object & gt; (); } If (! Html aviites.contentky ("class")) {html property. Add ("class", "form-control input-sm"); } Return html properties; }}}   

There is no change in your thinking - that is:

  @using (Html.BeginForm ()) {@ Html.TextBoxFor (m = & Gt; m.SomeProperty)}   

Generate a text box with your "Form-Control Input-SM" CSS class.

Comments

Popular posts from this blog

Java - Error: no suitable method found for add(int, java.lang.String) -

java - JPA TypedQuery: Parameter value element did not match expected type -

c++ - static template member variable has internal linkage but is not defined -