首页 > 学院 > 开发设计 > 正文

MVC MODEL Attribute 操纵速记

2019-11-17 02:02:14
字体:
来源:转载
供稿:网友

MVC MODEL Attribute 操纵速记

目的:

扩充Attribute

任意读取并Render 需要的Attribute

 

 

用法:

@Html.ParaLabelFor(x=>x.ServiceName):@Html.ParaNameFor(x=>x.ServiceName)

 

 

 

 

代码:

 

usingSystem;

usingSystem.Collections.Generic;

usingSystem.Linq;

usingSystem.Web;

usingSystem.Web.Mvc;

 

namespaceCardLanPay

{

publicstaticclassMyHtmlHelper

{

//public static System.Web.Mvc.MvcHtmlString DisplayParaName(this System.Web.Mvc.HtmlHelper helper, string paraName)

//{

// return System.Web.Mvc.MvcHtmlString.Create(Resources.AlipayParaNameResource.ResourceManager.GetString(paraName));

//}

 

publicstaticSystem.Web.Mvc.MvcHtmlStringParaNameFor<TModel, TValue>(thisSystem.Web.Mvc.HtmlHelper<TModel>self, System.Linq.ExPRessions.Expression<Func<TModel, TValue>>expression)

{

varmetadata=ModelMetadata.FromLambdaExpression(expression, self.ViewData);

varPropertyName=metadata.PropertyName;

if (PropertyName==null)

returnMvcHtmlString.Create("");

switch (metadata.ContainerType.FullName)

{

case"CardLanPay.Areas.Alipay.Models.BaseParaModel":

returnMvcHtmlString.Create(string.Format(@"<span>{0}</span>", Resources.AlipayParaNameResource.ResourceManager.GetString(PropertyName)));

default:

returnMvcHtmlString.Empty;

}

}

publicstaticSystem.Web.Mvc.MvcHtmlStringParaTextFor<TModel, TValue>(thisSystem.Web.Mvc.HtmlHelper<TModel>self, System.Linq.Expressions.Expression<Func<TModel, TValue>>expression)

{

varmetadata=ModelMetadata.FromLambdaExpression(expression, self.ViewData);

varPropertyName=metadata.PropertyName;

if (PropertyName==null)

returnMvcHtmlString.Empty;

switch (metadata.ContainerType.FullName)

{

case"CardLanPay.Areas.Alipay.Models.BaseParaModel":

returnMvcHtmlString.Create(string.Format(@"<span>{0}</span>", Resources.AlipayParaDisplayNameResource.ResourceManager.GetString(PropertyName)));

default:

returnMvcHtmlString.Empty;

}

}

 

publicstaticSystem.Web.Mvc.MvcHtmlStringParaLabelFor<TModel, TValue>(thisSystem.Web.Mvc.HtmlHelper<TModel>self, System.Linq.Expressions.Expression<Func<TModel, TValue>>expression)

{

 

varmetadata=ModelMetadata.FromLambdaExpression(expression, self.ViewData);

varPropertyName=metadata.PropertyName;

if (PropertyName==null)

returnMvcHtmlString.Empty;

stringtext="";

switch (metadata.ContainerType.FullName)

{

case"CardLanPay.Areas.Alipay.Models.BaseParaModel":

text=Resources.AlipayParaDisplayNameResource.ResourceManager.GetString(PropertyName);

break;

default:

break;

}

if (string.IsNullOrEmpty(text))

{

returnMvcHtmlString.Empty;

 

}

else

{

TagBuildertagBuilder=newTagBuilder("label");

tagBuilder.Attributes.Add("for", TagBuilder.CreateSanitizedId(self.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(PropertyName)));

tagBuilder.SetInnerText(text);

returnMvcHtmlString.Create(tagBuilder.ToString());

 

}

}

 

 

}

 

 

#region属性元数据管理

 

publicabstractclassMetadataAttribute : Attribute

{

publicabstractvoidProcess(ModelMetadatamodelMetaData);

}

 

publicclassParaNameModelMetadataProvider : DataAnnotationsModelMetadataProvider

{

protectedoverrideModelMetadataCreateMetadata(

IEnumerable<Attribute>attributes,

TypecontainerType,

Func<object>modelaccessor,

TypemodelType,

stringpropertyName)

{

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

attributes.OfType<ParaNameAttribute>().ToList().ForEach(x=>x.Process(modelMetadata));

returnmodelMetadata;

}

}

 

 

/// <summary>

/// 参数名特性类

/// </summary>

/// <remarks>

/// 用于给软件内部的属性指定支付宝接口使用的参数名

/// </remarks>

publicclassParaNameAttribute : MetadataAttribute

{

 

privatereadonlystringparaName;

 

publicParaNameAttribute(stringparaName)

{

this.paraName=paraName;

}

 

publicstringParaName

{

get { returnparaName; }

}

 

publicoverridevoidProcess(ModelMetadatamodelMetaData)

{

modelMetaData.AdditionalValues.Add("ParaName", Resources.AlipayParaNameResource.ResourceManager.GetString(ParaName));

}

 

}

 

#endregion

 

 

publicclassParaTextAttribute : System.ComponentModel.DisplayNameAttribute

{

publicParaTextAttribute(stringdisplayName)

{

DisplayNameValue=Resources.AlipayParaDisplayNameResource.ResourceManager.GetString(displayName);

}

}

}

 

 

 

参考:

http://stackoverflow.com/questions/3885796/get-displayname-attribute-without-using-labelfor-helper-in-asp.net-mvc

 

http://blog.csdn.net/li_xiao_ming/article/details/10134177

 

http://www.VEVb.com/wlitsoft/archive/2012/11/30/mvc-custom-htmlhelper.html


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表