MVC htmlhelp class extension for lambda expression method, htmlhelplam.pdf
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", "enter" + disp. Name );
// Verify
Foreach (var item in vilidate. GetInfo ())
{
Tag. MergeAttribute (item. Key, item. Value );
}
// Add
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 not set ":"";
Return new MvcHtmlString (tag. ToString ());
}