Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to create an HTML Helper to Extend TextBoxFor() to add CSS style?

how to create an HTML Helper to Extend TextBoxFor() to add CSS style?

@Html.TextBoxFor(model => model.FirstName, new { @class = "txt" }) 
like image 865
user584018 Avatar asked Jun 27 '12 17:06

user584018


2 Answers

You just need to create an extension method on HtmlHelper:

public static class MyHtmlHelpers
{
    public static MvcHtmlString MyTextBoxFor<TModel, TProperty>(
         this HtmlHelper<TModel> helper, 
         Expression<Func<TModel, TProperty>> expression)
    {
        return helper.TextBoxFor(expression, new { @class = "txt" });
    }
}

Then in your view you can use it as:

@Html.MyTextBoxFor(model => model.FirstName) 

Note: Don't forget to @using the namespace of MyHtmlHelpers if your views.

like image 177
nemesv Avatar answered Sep 17 '22 20:09

nemesv


    public static System.Web.Mvc.MvcHtmlString DtxTextBoxFor<TModel, TValue>
        (this System.Web.Mvc.HtmlHelper<TModel> html,
        System.Linq.Expressions.Expression<System.Func<TModel, TValue>> expression,
        System.Collections.Generic.IDictionary<string, object> htmlAttributes = null, bool readOnly = false)
    {
        if (htmlAttributes == null)
        {
            htmlAttributes =
                new System.Collections.Generic.Dictionary<string, object>();
        }

        System.Web.Mvc.ModelMetadata oModelMetadata =
            System.Web.Mvc.ModelMetadata.FromLambdaExpression(expression, html.ViewData);

        if (oModelMetadata == null)
        {
            if (readOnly)
            {
                if (htmlAttributes.ContainsKey("readonly") == false)
                {
                    htmlAttributes.Add("readonly", "read-only");
                }
            }
        }
        else
        {
            if (htmlAttributes.ContainsKey("placeholder") == false)
            {
                string strHtmlFieldName =
                    System.Web.Mvc.ExpressionHelper.GetExpressionText(expression);

                string strLabelText =
                    oModelMetadata.DisplayName ??
                    oModelMetadata.PropertyName ??
                    strHtmlFieldName.Split('.').Last();

                if (string.IsNullOrEmpty(strLabelText) == false)
                {
                    htmlAttributes.Add("placeholder", strLabelText);
                }
            }

            if ((readOnly) || (oModelMetadata.IsReadOnly))
            {
                if (htmlAttributes.ContainsKey("readonly") == false)
                {
                    htmlAttributes.Add("readonly", "read-only");
                }
            }
        }

        htmlAttributes.Add("class", "form-control");

        System.Linq.Expressions.MemberExpression oMemberExpression =
            expression.Body as System.Linq.Expressions.MemberExpression;

        if (oMemberExpression != null)
        {
            System.ComponentModel.DataAnnotations.StringLengthAttribute oStringLengthAttribute =
                oMemberExpression.Member.GetCustomAttributes
                (typeof(System.ComponentModel.DataAnnotations.StringLengthAttribute), false)
                .FirstOrDefault() as System.ComponentModel.DataAnnotations.StringLengthAttribute;

            if (oStringLengthAttribute != null)
            {
                if (htmlAttributes.ContainsKey("maxlength") == false)
                {
                    htmlAttributes.Add("maxlength", oStringLengthAttribute.MaximumLength);
                }
            }
        }

        return (html.TextBoxFor(expression, htmlAttributes));
    }
like image 32
Dariush Tasdighi Avatar answered Sep 19 '22 20:09

Dariush Tasdighi