从模型描述title属性创建CheckboxFor MVC帮手 [英] Create CheckboxFor MVC helper with title attribute from model description

查看:257
本文介绍了从模型描述title属性创建CheckboxFor MVC帮手的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个文本框助手从添加描述属性在模型采取领域的标题(提示):

 公共静态MvcHtmlString TextBoxForWithTitle<的tModel,TProperty>(此的HtmlHelper< tModel的>的HtmlHelper,防爆pression< Func键<的tModel,TProperty>>前pression,对象htmlAttributes = NULL)
    {
        VAR元= ModelMetadata.FromLambdaEx pression(如pression,htmlHelper.ViewData);
        字符串htmlFieldName =前pressionHelper.GetEx pressionText(如pression);
        字符串textboxText = metaData.DisplayName? metaData.PropertyName? htmlFieldName.Split('。')最后()。
        如果(string.IsNullOrEmpty(textboxText))
            返回MvcHtmlString.Empty;
        VAR文本框=新TagBuilder(输入);
        textbox.MergeAttributes(新RouteValueDictionary(htmlAttributes));
        如果(!string.IsNullOrEmpty(metaData.Description))
            textbox.Attributes.Add(称号,metaData.Description);
        返回MvcHtmlString.Create(textbox.ToString());
    }

我知道复选框,也是一个输入类型的元素,但我不知道如何构建一个辅助使用说明作为标题。

 公共静态MvcHtmlString CheckBoxForWithTitle<的TModel,TProperty>(此的HtmlHelper<的TModel>的HtmlHelper,防爆pression< Func键<的TModel,TProperty>>前pression,对象htmlAttributes = NULL)
    {
        VAR元= ModelMetadata.FromLambdaEx pression(如pression,htmlHelper.ViewData);
        字符串htmlFieldName =前pressionHelper.GetEx pressionText(如pression);
        字符串chkboxText = metaData.DisplayName? metaData.PropertyName? htmlFieldName.Split('。')最后()。
        MemberEx pression memberEx pression =前pression.Body作为MemberEx pression;
        字符串参数名称= memberEx pression.Member.Name;        如果(string.IsNullOrEmpty(chkboxText))
            返回MvcHtmlString.Empty;
        VAR chkbox =新Mv​​cHtmlString(
            的String.format(
            <输入类型= \\复选框\\NAME = \\{0} \\ID = \\{0} \\价值= \\{1} \\{2} />中,
            参数名称,
        chkbox.MergeAttributes(新RouteValueDictionary(htmlAttributes));
        如果(!string.IsNullOrEmpty(metaData.Description))
            chkbox.Attributes.Add(称号,metaData.Description);
        返回MvcHtmlString.Create(chkbox.ToString());
    }


解决方案

您目前的实现都没有考虑到模型绑定和的ModelState ,不会产生必要的属性不引人注目的验证和可以产生不正确 ID 属性。

请使用您自己的助手现有的HTML辅助方法让你产生正确的HTML。你的 TextBoxForWithTitle()方法只需要

 公共静态MvcHtmlString TextBoxForWithTitle<的tModel,TProperty>(此的HtmlHelper< tModel的>的HtmlHelper,防爆pression< Func键<的tModel,TProperty>>前pression,对象htmlAttributes = NULL)
{
    VAR元= ModelMetadata.FromLambdaEx pression(如pression,htmlHelper.ViewData);
    IDictionary的<字符串对象>属性= HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes);
    如果(!string.IsNullOrEmpty(metaData.Description))
    {
        attributes.Add(称号,metaData.Description);
    }
    返回htmlHelper.TextBoxFor(如pression,属性);
}

和类似的 CheckBoxForWithTitle()将是相同的,除了

 返回htmlHelper.CheckBoxFor(如pression,属性);

边注:要查看现有的助手是如何工作的,你可以查看源$ C ​​$ C <一个href=\"https://aspnetwebstack.$c$cplex.com/SourceControl/latest#src/System.Web.Mvc/Html/InputExtensions.cs\"相对=nofollow>这里

I've created a text box helper to add a title (tooltip) taken from the description attribute for the field in a model:

 public static MvcHtmlString TextBoxForWithTitle<Tmodel, TProperty>(this HtmlHelper<Tmodel> htmlHelper, Expression<Func<Tmodel, TProperty>> expression, object htmlAttributes = null)
    {
        var metaData = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
        string htmlFieldName = ExpressionHelper.GetExpressionText(expression);
        string textboxText = metaData.DisplayName ?? metaData.PropertyName ?? htmlFieldName.Split('.').Last();
        if (string.IsNullOrEmpty(textboxText))
            return MvcHtmlString.Empty;
        var textbox = new TagBuilder("input");
        textbox.MergeAttributes(new RouteValueDictionary(htmlAttributes));
        if (!string.IsNullOrEmpty(metaData.Description))
            textbox.Attributes.Add("title", metaData.Description);
        return MvcHtmlString.Create(textbox.ToString());
    }

I know the checkbox is also an 'input' type element but I have no idea how to construct a helper to use the description as a title.

 public static MvcHtmlString CheckBoxForWithTitle<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, object htmlAttributes = null)
    {
        var metaData = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
        string htmlFieldName = ExpressionHelper.GetExpressionText(expression);
        string chkboxText = metaData.DisplayName ?? metaData.PropertyName ?? htmlFieldName.Split('.').Last();
        MemberExpression memberExpression = expression.Body as MemberExpression;
        string parameterName = memberExpression.Member.Name;

        if (string.IsNullOrEmpty(chkboxText))
            return MvcHtmlString.Empty;
        var chkbox = new MvcHtmlString(
            string.Format(
            "<input type=\"checkbox\" name=\"{0}\" id=\"{0}\" value=\"{1}\" {2} />",
            parameterName, 
        chkbox.MergeAttributes(new RouteValueDictionary(htmlAttributes));
        if (!string.IsNullOrEmpty(metaData.Description))
            chkbox.Attributes.Add("title", metaData.Description);
        return MvcHtmlString.Create(chkbox.ToString());
    }

解决方案

You current implementations are not taking into account model binding and ModelState, do not generate the attributes necessary for unobtrusive validation and can generate incorrect id attributes.

Make use of the existing html helper methods in your own helpers so you generate the correct html. Your TextBoxForWithTitle() method need only be

public static MvcHtmlString TextBoxForWithTitle<Tmodel, TProperty>(this HtmlHelper<Tmodel> htmlHelper, Expression<Func<Tmodel, TProperty>> expression, object htmlAttributes = null)
{
    var metaData = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
    IDictionary<string, object> attributes = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes);
    if (!string.IsNullOrEmpty(metaData.Description))
    {
        attributes.Add("title", metaData.Description);
    }
    return htmlHelper.TextBoxFor(expression, attributes);
}

and similarly the CheckBoxForWithTitle() would be the same except

return htmlHelper.CheckBoxFor(expression, attributes);

Side note: To see how the existing helpers actually work, you can view the source code here

这篇关于从模型描述title属性创建CheckboxFor MVC帮手的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆