扩展HTML助手 [英] Extending HTML Helper

查看:123
本文介绍了扩展HTML助手的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在code我期待产生类似:

The code I am looking to produce is similar to:

<div class="form-group">
    <label for="email">Email address</label>
    <input type="email" class="form-control" id="email" placeholder="Enter email">
</div>

使用这样的:

@Html.LabelledTextBoxFor(model => model.EmailAddress)

当标签文本得到来自model.EmailAddress衍生

Where the label text get derived from model.EmailAddress

这将使用数据注释来完成例如。

This would be done using Data Annotation e.g.

[Displayname("Email Address]

什么是实现这一目标的最佳方式?

What is the best way to accomplish this?

这是否会使用jQuery VAL影响自动生成的客户端JS验证?

Will this affect the auto generated client side JS validation using jquery val?

推荐答案

我写了一个扩展方法,这也许可以帮助:

I wrote an extension method, maybe this might help:

public static MvcHtmlString LabelledTextBoxFor<TModel, TResult>(this HtmlHelper<TModel> html, Expression<Func<TModel, TResult>> expression)
{
    ExpressionType type = expression.Body.NodeType;
    if (type == ExpressionType.MemberAccess)
    {
       MemberExpression memberExpression = (MemberExpression) expression.Body;
       var propName = memberExpression.Member.Name;

       var member = memberExpression.Member as PropertyInfo;

       var attributes = member.GetCustomAttributes();

       StringBuilder sb = new StringBuilder();
       foreach (var attribute in attributes)
       {
           if (attribute is DisplayAttribute)
           {
               DisplayAttribute d = attribute as DisplayAttribute;
               var displayName = d.Name;
               sb.Append("<div class=\"form-group\">");
               sb.AppendFormat("<label for=\"{0}\">{1}</label>", propName, displayName);
               sb.AppendFormat(
                        "<input type=\"email\" class=\"form-control\" id=\"{0}\" placeholder=\"Enter email\">",
                        propName);
               sb.Append("</div>");
               return MvcHtmlString.Create(sb.ToString());
             }
         }

     }
       return MvcHtmlString.Create("");
 }

您可以使用默认的显示属性来指定显示name.There无需定制attributes.And你可以使用这个扩展是这样的:

You can use default display attribute to specify display name.There is no need for custom attributes.And you can use this extension like this:

@Html.LabelledTextBoxFor(model => model.EmailAddress)

请注意:我曾尝试自己和它的正常工作

Note: I have tried myself and it is working correctly.

更新:更简单的版本。

public static MvcHtmlString LabelledTextBoxFor2<TModel, TResult>(this HtmlHelper<TModel> html, Expression<Func<TModel, TResult>> expression)
    {
        ExpressionType type = expression.Body.NodeType;
        if (type == ExpressionType.MemberAccess)
        {
            var metadata = ModelMetadata.FromLambdaExpression(expression, html.ViewData);
            var displayName = metadata.DisplayName;
            var propName = metadata.PropertyName;

            StringBuilder sb = new StringBuilder();

            sb.Append("<div class=\"form-group\">");
            sb.AppendFormat("<label for=\"{0}\">{1}</label>", propName, displayName);
            sb.AppendFormat(
                        "<input type=\"email\" class=\"form-control\" id=\"{0}\" placeholder=\"Enter email\">",
                        propName);
             sb.Append("</div>");
             return MvcHtmlString.Create(sb.ToString());
        }

        return MvcHtmlString.Create("");
    }

这篇关于扩展HTML助手的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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