Skip to content

Instantly share code, notes, and snippets.

@andreabalducci
Created January 10, 2011 22:36
Show Gist options
  • Save andreabalducci/773608 to your computer and use it in GitHub Desktop.
Save andreabalducci/773608 to your computer and use it in GitHub Desktop.
Uppercase and Lowercase ModelBinder attributes for Asp.Net MVC2
...
protected void Application_Start()
{
...
// todo: add windsor + autodiscovery by convention
ModelMetadataProviders.Current = new LucillaMetadataProvider();
ModelBinders.Binders.DefaultBinder = new LucillaModelBinder();
...
}
...
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using Lucilla.Framework.Web.MVC;
using Lucilla.Framework.Web.MVC.Annotations;
using Studio.Models.Shared;
namespace Studio.Models.Customer
{
public class InsertCustomerModel : MasterPageModel
{
public int Id { get; set; }
[Required]
[Uppercase]
public string Customer { get; set; }
[Required]
public string CompanyName{ get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Lucilla.Framework.Web.MVC.Converters
{
public interface IValueConverter
{
string Name { get; }
object ApplyConversion(object source);
}
public interface IValueConverter<in TSource, out TConverted> : IValueConverter
{
TConverted ApplyConversion(TSource source);
}
}
using System;
using Lucilla.Framework.Web.MVC.Converters;
namespace Lucilla.Framework.Web.MVC.Annotations
{
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
public class LowercaseAttribute : TextTransformAttribute
{
public override string GetConverterName()
{
return LowercaseConverter.Name;
}
}
}
using System;
namespace Lucilla.Framework.Web.MVC.Converters
{
public class LowercaseConverter : IValueConverter<string, string>
{
public string ApplyConversion(string source)
{
return String.IsNullOrEmpty(source) ? source : source.ToLowerInvariant();
}
public static string Name
{
get { return "text-transform:lowercase"; }
}
string IValueConverter.Name
{
get { return Name; }
}
public object ApplyConversion(object source)
{
return ApplyConversion((string) source);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using Lucilla.Framework.Web.MVC.Annotations;
namespace Lucilla.Framework.Web.MVC
{
public class LucillaMetadataProvider : DataAnnotationsModelMetadataProvider
{
protected override ModelMetadata CreateMetadata(IEnumerable<Attribute> attributes, Type containerType, Func<object> modelAccessor, Type modelType, string propertyName)
{
var metadata = base.CreateMetadata(attributes, containerType, modelAccessor, modelType, propertyName);
var caseAttrib = attributes.OfType<TextTransformAttribute>().FirstOrDefault();
if (caseAttrib != null)
{
metadata.AdditionalValues.Add("text-transform", caseAttrib.GetConverterName());
}
return metadata;
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using Lucilla.Framework.Web.MVC.Converters;
namespace Lucilla.Framework.Web.MVC
{
public class LucillaModelBinder : DefaultModelBinder
{
private Dictionary<string, IValueConverter> _converters = new Dictionary<string, IValueConverter>();
public LucillaModelBinder()
{
AddConverter(new UppercaseConverter());
AddConverter(new LowercaseConverter());
}
protected override object GetPropertyValue(ControllerContext controllerContext, ModelBindingContext bindingContext, PropertyDescriptor propertyDescriptor, IModelBinder propertyBinder)
{
object value = base.GetPropertyValue(controllerContext, bindingContext, propertyDescriptor, propertyBinder);
foreach (var key in bindingContext.ModelMetadata.AdditionalValues.Keys)
{
string additionalValue = bindingContext.ModelMetadata.AdditionalValues[key] as string;
if (additionalValue != null && _converters.ContainsKey(additionalValue))
{
value = _converters[additionalValue].ApplyConversion(value);
}
}
return value;
}
public LucillaModelBinder AddConverter(IValueConverter converter)
{
this._converters.Add(converter.Name, converter);
return this;
}
}
}
using System;
namespace Lucilla.Framework.Web.MVC.Annotations
{
public abstract class TextTransformAttribute : Attribute
{
public abstract string GetConverterName();
}
}
using System;
using Lucilla.Framework.Web.MVC.Converters;
namespace Lucilla.Framework.Web.MVC.Annotations
{
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
public class UppercaseAttribute : TextTransformAttribute
{
public override string GetConverterName()
{
return UppercaseConverter.Name;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Lucilla.Framework.Web.MVC.Converters
{
public class UppercaseConverter : IValueConverter<string, string>
{
public string ApplyConversion(string source)
{
return String.IsNullOrEmpty(source) ? source : source.ToUpperInvariant();
}
public static string Name
{
get { return "text-transform:uppercase"; }
}
string IValueConverter.Name
{
get { return Name; }
}
public object ApplyConversion(object source)
{
return ApplyConversion((string) source);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment