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

asp.net mvc 2自定义DropdownList控件

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

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 { get; set; } 
通过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); 
        }  
  (责任编辑:admin)

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