I want all dates on my system to be valid, and not in the future, so I force them inside the binding to a custom model:
class DateTimeModelBinder : IModelBinder { public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { var value = bindingContext.ValueProvider.GetValue(bindingContext.ModelName); try { var date = value.ConvertTo(typeof(DateTime), CultureInfo.CurrentCulture);
Now, with a few exceptions, I want some dates to be in the future
[Required] [Display(Name = "Future Date")] [DataType(DataType.DateTime)] [FutureDateTime] <-- this attribute should allow the exception public DateTime FutureFecha { get; set; }
This attribute
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = false)] public class FutureDateTimeAttribute : Attribute { }
Now the question is: How to check if an attribute is present inside the BindModel method?
c # asp.net-mvc model-binding
Eduardo molteni
source share