惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

量子位
C
CXSECURITY Database RSS Feed - CXSecurity.com
S
Schneier on Security
博客园 - 叶小钗
博客园 - 三生石上(FineUI控件)
C
Cybersecurity and Infrastructure Security Agency CISA
Engineering at Meta
Engineering at Meta
Google DeepMind News
Google DeepMind News
酷 壳 – CoolShell
酷 壳 – CoolShell
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园_首页
T
Threat Research - Cisco Blogs
C
Cisco Blogs
Recent Announcements
Recent Announcements
S
Securelist
N
Netflix TechBlog - Medium
The Register - Security
The Register - Security
P
Privacy & Cybersecurity Law Blog
宝玉的分享
宝玉的分享
D
Darknet – Hacking Tools, Hacker News & Cyber Security
L
LINUX DO - 热门话题
T
Tor Project blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
月光博客
月光博客
AWS News Blog
AWS News Blog
P
Proofpoint News Feed
博客园 - 司徒正美
L
LINUX DO - 最新话题
Stack Overflow Blog
Stack Overflow Blog
博客园 - 聂微东
H
Help Net Security
Spread Privacy
Spread Privacy
PCI Perspectives
PCI Perspectives
Project Zero
Project Zero
I
Intezer
T
The Blog of Author Tim Ferriss
有赞技术团队
有赞技术团队
The Last Watchdog
The Last Watchdog
C
Check Point Blog
Blog — PlanetScale
Blog — PlanetScale
B
Blog RSS Feed
MyScale Blog
MyScale Blog
V
Vulnerabilities – Threatpost
Recorded Future
Recorded Future
T
Tenable Blog
Jina AI
Jina AI
D
DataBreaches.Net
阮一峰的网络日志
阮一峰的网络日志

博客园 - 王继坤

DataTable 递归 简单的程序,来实现无限级列表 结合 jquery.table.js 实现 asp.net mvc 2 简简单单做开发 使用DataContext扩展方法Find<TEntity>(TEntity obj) 遇到的问题 asp.net mvc 2 简简单单做开发 实现基本数据操作操作RepositoryController<T> asp.net mvc 2 简简单单做开发 自定义Controller基类 - 王继坤 asp.net mvc 2 简简单单做开发 通用增删改基本操作通用页面 asp.net mvc 2 DisplayTemplates 的使用 FcDigg 源码 asp.net 3.5 linq to sql 扩展方法 linq to access 简单实现 实例demo linq to access 简单实现 asp.net mvc 随想 asp.net mvc ajax实现1 asp.net ajax fccms 小型简单个人blog源码 FCKEDITOR中文使用说明 js调用 dotfuscator.exe 采用线程生成静态页面 新的开始--- gridview 实现多字段综合查询
asp.net mvc 2 简简单单做开发 自定义DropdownList控件
王继坤 · 2010-07-03 · via 博客园 - 王继坤

  asp.net mvc 2 给我们提供了强大的自定义功能,今天主要说下DropdownList自定义绑定字段显示,通过ViewData设定DropdownList的数据项。自动绑定显示。实现的方式。在global.asax 中注册 FieldTemplateMetadataProvider,

 ModelMetadataProviders.Current = new mvc.Models.FieldTemplateMetadataProvider();
通过返回的 FieldTemplateMetadata 。在MetaData中指定使用DropDownList的字段
       [Display( Name="",Order=12)]
        [Required]
        [SearchFilter]
        [DisplayName("栏目")]
        [DropDownList("Category""Id""Name")]
        public int Cid { getset; }

通过DropDownList指定调用的模板为dropdownlist.ascx ,在dropdownlist.ascx 将默认的 ModelMetadata 转成FieldTemplateMetadata 获取 DropDownListAttribute 。

<script runat="server">
    DropDownListAttribute GetDropDownListAttribute()
    {
        FieldTemplateMetadata metaData = ViewData.ModelMetadata as FieldTemplateMetadata;

        return (metaData != null) ? metaData.Attributes.OfType<DropDownListAttribute>().SingleOrDefault() : null;
    }
</script>

 
  通过DropDownListAttribute 获得 ViewData的key ,绑定的文本对应的字段,值对应的字段,使用html.DropDownlist显示数据

    DropdownList.ascx 代码

代码

<%@ Import Namespace="mvc.Models"%>
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<script runat="server">
    DropDownListAttribute GetDropDownListAttribute()
    {
        FieldTemplateMetadata metaData 
= ViewData.ModelMetadata as FieldTemplateMetadata;return (metaData != null? metaData.Attributes.OfType<DropDownListAttribute>().SingleOrDefault() : null;
    }
</script>
<% DropDownListAttribute attribute = GetDropDownListAttribute();%>
<% if (attribute != null) {%>
    
<%= Html.DropDownList(string.Empty, new SelectList(ViewData[attribute.ViewDataKey] as IEnumerable, attribute.DataValueField, attribute.DataTextField, Model), attribute.OptionLabel, attribute.HtmlAttributes) %>
<% }%>
<% else {%>
    
<%= Html.DisplayForModel() %>
<% }%>

自定义DropDownListAttribute 属性

代码

namespace mvc.Models
{
    
using System;
    
using System.Collections.Generic;
    
using System.ComponentModel;
    
using System.ComponentModel.DataAnnotations;
    
using System.Linq;
    
using System.Web.Routing;

    [AttributeUsage(AttributeTargets.Property, AllowMultiple 

= false, Inherited = true)]
    
public sealed class DropDownListAttribute : Attribute, ITemplateField
    {
        
private static string defaultTemplateName;public DropDownListAttribute(string viewDataKey, string dataValueField) : this(viewDataKey, dataValueField, null)
        {
        }
public DropDownListAttribute(string viewDataKey, string dataValueField, string dataTextField) : this(viewDataKey, dataValueField, dataTextField, null)
        {
        }
public DropDownListAttribute(string viewDataKey, string dataValueField, string dataTextField, string optionLabel) : this(DefaultTemplateName, viewDataKey, dataValueField, dataTextField, optionLabel, null)
        {
        }
public DropDownListAttribute(string viewDataKey, string dataValueField, string dataTextField, string optionLabel, object htmlAttributes) : this(DefaultTemplateName, viewDataKey, dataValueField, dataTextField, optionLabel, htmlAttributes)
        {
        }
public DropDownListAttribute(string templateName, string viewDataKey, string dataValueField, string dataTextField, string optionLabel, object htmlAttributes)
        {
            
if (string.IsNullOrEmpty(templateName))
            {
                
throw new ArgumentException("Template name cannot be empty.");
            }
if (string.IsNullOrEmpty(viewDataKey))
            {
                
throw new ArgumentException("View data key cannot be empty.");
            }
if (string.IsNullOrEmpty(dataValueField))
            {
                
throw new ArgumentException("Data value field cannot be empty.");
            }

            TemplateName 

= templateName;
            ViewDataKey 
= viewDataKey;
            DataValueField 
= dataValueField;
            DataTextField 
= dataTextField;
            OptionLabel 
= optionLabel;
            HtmlAttributes 
= new RouteValueDictionary(htmlAttributes);
        }
public static string DefaultTemplateName
        {
            
get
            {
                
if (string.IsNullOrEmpty(defaultTemplateName))
                {
                    defaultTemplateName 
= "DropDownList";
                }
return defaultTemplateName;
            }
            
set
            {
                defaultTemplateName 
= value;
            }
        }
public string TemplateName { getprivate set; }public string ViewDataKey { getprivate set; }public string DataValueField { getprivate set; }public string DataTextField { getprivate set; }public string OptionLabel { getprivate set; }public IDictionary<stringobject> HtmlAttributes { getprivate 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 { getset; }
    }

自定义 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;
        }
    }