Asp.Net教程,WinForm教程,Asp.Net MVC,vs2008教程,vs2010教程,Silverlight技术,源码下载,Asp.Net视频教程
全站热门标签
vs2010 Silverlight 存储过程 水晶报表 LINQ ADO.NET JavaScript DataGridView GridView AjaxPro DevExpress 面向对象 Extjs XML HTML教程 Oracle jQuery WPF MVC 分页 Office2010 GDI+ Visual C++2010 Dojo MySQL VB.NET WCF4.0 ASPxGridView WinForm textbox Sql2005 cookie Discuz!NT checkbox WCF SQL经典语句 T-SQL F# asp.net VS2008 SQL Ajax DropDownList VS2008新特性 TreeView Access Flex 页面执行时间 字符串 DataSet VB2005 回调 动画 C#时间 游戏 ASP.NET MVC
当前位置: 主页 > Mvc技术 >

asp.net mvc 2自定义DropdownList控件(2)

时间:2010-08-15 21:31来源:未知 作者:admin 点击:

        public static string DefaultTemplateName 
        { 
            get 
            { 
                if (string.IsNullOrEmpty(defaultTemplateName)) 
                { 
                    defaultTemplateName = "DropDownList"; 
                } 
 
                return defaultTemplateName; 
            } 
            set 
            { 
                defaultTemplateName = value; 
            } 
        } 
 
        public string TemplateName { get; private set; } 
 
        public string ViewDataKey { get; private set; } 
 
        public string DataValueField { get; private set; } 
 
        public string DataTextField { get; private set; } 
 
        public string OptionLabel { get; private set; } 
 
        public IDictionary<string, object> HtmlAttributes { get; private set; } 
 
        public object GetSelectedValue(object model) 
        { 
            return GetPropertyValue(model, DataValueField); 
        } 
 
        public object GetSelectedText(object model) 
        { 
            return GetPropertyValue(model, !string.IsNullOrEmpty(DataTextField) ? DataTextField : DataValueField); 
        } 
 
        private static object GetPropertyValue(object model, string propertyName) 
        { 
            if (model != null) 
            { 
                PropertyDescriptor property = GetTypeDescriptor(model.GetType()).GetProperties() 
                                                                                .Cast<PropertyDescriptor>() 
                                                                                .SingleOrDefault(p => string.Compare(p.Name, propertyName, StringComparison.OrdinalIgnoreCase) == 0); 
                
                if (property != null) 
                { 
                    return property.GetValue(model); 
                } 
            } 
 
            return null; 
        } 
 
        private static ICustomTypeDescriptor GetTypeDescriptor(Type type) 
        { 
            return new AssociatedMetadataTypeTypeDescriptionProvider(type).GetTypeDescriptor(type); 
        } 
    } 

自定义DataAnnotationsModelMetadata

public class FieldTemplateMetadata : DataAnnotationsModelMetadata 
    { 
        public FieldTemplateMetadata(DisplayAttribute aa, DataAnnotationsModelMetadataProvider provider, Type containerType, Func<object> modelAccessor, Type modelType, string propertyName, DisplayColumnAttribute displayColumnAttribute, IEnumerable<Attribute> attributes) : base(provider, containerType, modelAccessor, modelType, propertyName, displayColumnAttribute) 
        { 
            Attributes = new List<Attribute>(attributes); 
            Display = aa; 
        } 
 
        public IList<Attribute> Attributes 
        { 
            get; 
            private set; 
        } 
        public DisplayAttribute Display { get; set; } 
    } 
自定义 DataAnnotationsModelMetadataProvider

public class FieldTemplateMetadataProvider : DataAnnotationsModelMetadataProvider 
    { 
        protected override ModelMetadata CreateMetadata(IEnumerable<Attribute> attributes, Type containerType, Func<object> modelAccessor, Type modelType, string propertyName) 
        { 
            DataAnnotationsModelMetadata result = (DataAnnotationsModelMetadata) base.CreateMetadata(attributes, containerType, modelAccessor, modelType, propertyName); 
 
            string templateName = attributes.OfType<ITemplateField>() 
                                            .Select(field => field.TemplateName) 
                                            .LastOrDefault(); 
            List<System.Attribute> attributeList = new List<System.Attribute>(attributes); 
            DisplayAttribute disp = attributeList.OfType<DisplayAttribute>().FirstOrDefault(); 
            if (disp != null) 
            { 
                result.ShortDisplayName = disp.Order.ToString(); ; 
                result.Description = disp.Description; 
            } 
 
            
 
            var data= (new FieldTemplateMetadata(disp, this, containerType, modelAccessor, modelType, propertyName, attributes.OfType<DisplayColumnAttribute>().FirstOrDefault(), attributes) 
 
                    { 
                        TemplateHint = !string.IsNullOrEmpty(templateName) ? templateName : result.TemplateHint, 
                        HideSurroundingHtml = result.HideSurroundingHtml, 
                        DataTypeName = result.DataTypeName, 
                        IsReadOnly = result.IsReadOnly, 
                        NullDisplayText = result.NullDisplayText, 
                        DisplayFormatString = result.DisplayFormatString, 
                        ConvertEmptyStringToNull = result.ConvertEmptyStringToNull, 
                        EditFormatString = result.EditFormatString, 
                        ShowForDisplay = result.ShowForDisplay, 
                        ShowForEdit = result.ShowForEdit, 
                        DisplayName = result.DisplayName, 
                        Description = result.Description, 
                        ShortDisplayName = result.ShortDisplayName, 
 
                    }); 
 
            SearchFilterAttribute searchFilterAttribute = attributes.OfType<SearchFilterAttribute>().FirstOrDefault(); 
            if (searchFilterAttribute != null) 
            { 
                data.AdditionalValues.Add("Search", searchFilterAttribute); 
            } 
 
            OrderByAttribute orderByAttribute = attributes.OfType<OrderByAttribute>().FirstOrDefault(); 
            if (orderByAttribute != null) 
            { 
                data.AdditionalValues.Add("OrderBy", orderByAttribute); 
            } 
            return data; 
        } 
    } 
 

Tags:ASP.NET MVC2 DropdownLis
责任编辑:admin
返回顶部
------分隔线----------------------------
推荐内容
骆驼户外男 真皮磨砂日常休闲鞋 低帮 2011秋冬新款 专柜正品特价 骆驼户外男 真皮磨砂日常休闲鞋 低帮 2011秋冬新款 专柜正品特价