html description长度,Html.DescriptionFor

这不是那么简单,因为DataAnnotationsModelMetadataProvider(名称!)不使用Description属性。

因此,要使用Description属性,您需要执行以下操作:

创建自定义ModelMetaDataProvider。

将其注册为应用程序的元数据提供程序。

实施DescriptionFor方法。

所以...

这是自定义ModelMetaDataProvider:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Mvc;

using System.ComponentModel;

namespace MvcApplication2

{

public class DescriptionModelMetaDataProvider : DataAnnotationsModelMetadataProvider

{

protected override ModelMetadata CreateMetadata(IEnumerable attributes, Type containerType, Func modelAccessor, Type modelType, string propertyName)

{

ModelMetadata result = base.CreateMetadata(attributes, containerType, modelAccessor, modelType, propertyName);

DescriptionAttribute descriptionAttribute = attributes.OfType().FirstOrDefault();

if (descriptionAttribute != null)

result.Description = descriptionAttribute.Description;

return result;

}

}

}现在,注册它是在Application_Start方法中的Global.asax文件中完成的,如下所示:

protected void Application_Start()

{

AreaRegistration.RegisterAllAreas();

RegisterRoutes(RouteTable.Routes);

// This is the important line:

ModelMetadataProviders.Current = new DescriptionModelMetaDataProvider();

}最终,DescriptionFor方法的实现:

using System.Linq.Expressions;

namespace System.Web.Mvc.Html

{

public static class Helper

{

public static MvcHtmlString DescriptionFor(this HtmlHelper html, Expression> expression)

{

ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, html.ViewData);

string fieldName = ExpressionHelper.GetExpressionText(expression);

return MvcHtmlString.Create(String.Format("{1}",

html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldId(fieldName),

metadata.Description));// Here goes the description

}

}

}这应该工作,我在我的机器上检查了它。

吉文。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值