0

Os códigos fonte dos Helpers como por exemplo o do TextBoxFor estão disponíveis em algum lugar?

Já encontrei customizações como essa:

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));
    }

É que estudar os códigos originais, se estivessem disponíveis, seria de boa ajuda.

Além disso, uma dúvida, de quem são esses helpers que usamos a partir do @Html.? Do asp net? Do MVC5? Do Razor?

1 Resposta 1

0

Sim, ele está disponível em: https://referencesource.microsoft.com/#PresentationFramework/src/Framework/MS/Internal/Helper.cs

Assim como todo código do .NET Framework.

Mas acredito que isso não vai te ajudar muito a criar helpers. Existem vários tutoriais que podem te ajudar mais na internet:

https://docs.microsoft.com/en-us/aspnet/web-pages/overview/ui-layouts-and-themes/creating-and-using-a-helper-in-an-aspnet-web-pages-site

http://www.webdevelopmenthelp.net/2014/07/custom-html-helper-in-aspnet-mvc.html

Além disso, uma dúvida, de quem são esses helpers que usamos a partir do @Html.? Do asp net? Do MVC5? Do Razor?

São do MVC, como podes ver no fonte no link que passei acima.

1
  • Muito obrigado. Exatamente o que eu queria.
    – Neumann
    23/03/2018 às 13:58

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .