
























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)]TemplateName
= templateName;自定义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
});
SearchFilterAttribute searchFilterAttribute
= attributes.OfType<SearchFilterAttribute>().FirstOrDefault();OrderByAttribute orderByAttribute
= attributes.OfType<OrderByAttribute>().FirstOrDefault();此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。