首页 > 代码库 > MVC htmlhelp类扩展for lambda表达式方式
MVC htmlhelp类扩展for lambda表达式方式
public static MvcHtmlString InputTextFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression,object attr=null)
{
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());
}
{
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());
}
MVC htmlhelp类扩展for lambda表达式方式
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。