|
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;
}
}
|