我如何显示DisplayAttribute.Description属性值? [英] How do I display the DisplayAttribute.Description attribute value?

查看:430
本文介绍了我如何显示DisplayAttribute.Description属性值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个模型类,具有这样的属性:

I have a model class, with a property like this:

[Display(Name = "Phone", Description="Hello World!")]
public string Phone1 { get; set; }

显示一个标签和渲染的文本框在我看来输入为pretty容易:

Displaying a label and rendering a textbox for input in my view is pretty easy:

@Html.LabelFor(model => model.Organization.Phone1)
@Html.EditorFor(model => model.Organization.Phone1)
@Html.ValidationMessageFor(model => model.Organization.Phone1)

但我怎么渲染说明标注属性的值,即世界,你好!??

But how do I render the value of the Description annotation attribute, i.e. "Hello World!"??

推荐答案

我结束了这样的帮手:

using System;
using System.Linq.Expressions;
using System.Web.Mvc;

public static class MvcHtmlHelpers
{
    public static MvcHtmlString DescriptionFor<TModel, TValue>(this HtmlHelper<TModel> self, Expression<Func<TModel, TValue>> expression)
    {
        var metadata = ModelMetadata.FromLambdaExpression(expression, self.ViewData);
        var description = metadata.Description;

        return MvcHtmlString.Create(string.Format(@"<span>{0}</span>", description));
    }
}

感谢那些谁使我在正确的方向。 :)

Thanks to those who led me in the right direction. :)

这篇关于我如何显示DisplayAttribute.Description属性值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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