MVC htmlhelp类扩展for lambda表达式方式
{
DisplayAttribute disp = null;
VilidateAttribute vilidate = null;
string name = string.Empty;
try
{
dynamic exp = expression.Body.GetType().GetProperty("Member").GetValue(expression.Body, null);
var strName = (string)exp.Name;
var p = typeof(TModel).GetProperty(strName);
disp = p.GetCustomAttributes(typeof(DisplayAttribute), false).FirstOrDefault() as DisplayAttribute;
vilidate = p.GetCustomAttributes(typeof(VilidateAttribute), false).FirstOrDefault() as VilidateAttribute;
name = strName;
}
catch (Exception e)
{
}
TagBuilder tag = new TagBuilder("input");
tag.MergeAttribute("type", "text");
tag.MergeAttribute("class", "form-control");
tag.MergeAttribute("id","i-"+ name);
tag.MergeAttribute("name","n-"+ name);
tag.MergeAttribute("placeholder", "请输入" + disp.Name);
//验证
foreach (var item in vilidate.GetInfo())
{
tag.MergeAttribute(item.Key, item.Value);
}
//附加
if(attr!=null)
{
foreach (var item in attr.GetType().GetProperties(Reflection.BindingFlags.Instance | Reflection.BindingFlags.Public))
{
tag.MergeAttribute(item.Name, item.GetValue(attr, null).ToString());
}
}
tag.InnerHtml = disp == null ? "未设置Display" : "";
return new MvcHtmlString(tag.ToString());
}
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。