使用模型描述中的 title 属性创建 CheckboxFor MVC 帮助器

Create CheckboxFor MVC helper with title attribute from model description

我创建了一个文本框助手来添加一个标题(工具提示),该标题取自模型中字段的描述属性:

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

我知道复选框也是一个 'input' 类型的元素,但我不知道如何构建一个助手来使用描述作为标题。

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

我试过了,到目前为止它似乎有效 - 仍然需要尝试一些我需要元素 ID 的示例:

 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 TagBuilder("input");
        chkbox.Attributes.Add("type", "checkbox");
        chkbox.MergeAttributes(new RouteValueDictionary(htmlAttributes));
        if (!string.IsNullOrEmpty(metaData.Description))
            chkbox.Attributes.Add("title", metaData.Description);
        return MvcHtmlString.Create(chkbox.ToString());
    }

您当前的实现未考虑模型绑定和 ModelState,未生成非侵入性验证所需的属性,并且可能生成不正确的 id 属性。

在您自己的帮助器中使用现有的 html 帮助器方法,以便生成正确的 html。您的 TextBoxForWithTitle() 方法只需

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

类似地 CheckBoxForWithTitle() 除了

是一样的
return htmlHelper.CheckBoxFor(expression, attributes);

旁注:要了解现有助手的实际工作方式,您可以查看源代码here