MVC htmlhelp class extension for lambda expression mode

Source: Internet
Author: User

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", "Please enter" + disp. Name);
Verify
foreach (var item in Vilidate. GetInfo ())
{
Tag. Mergeattribute (item. Key, item. Value);
}
Additional
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 ());
}

MVC htmlhelp class extension for lambda expression mode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.