Asp.net mvc 2 ASP.NET mvc 2-使用下拉列表绑定出生日期
在ASP.NET MVC 2中,如果应用程序必须有3个下拉列表来选择月、日、年,那么如何绑定作为日期时间的视图模型属性呢?我很久以前读过Scott H.关于绑定日期的博客文章,对于这样一个简单的案例来说,这似乎太复杂了。肯定有更干净/更好的方法吗 无论我使用什么解决方案,我都希望使用DataAnnotations之类的东西保留内置验证,并且我还希望能够使用validation属性指定最小/最大日期范围 我首先想到的是一个简单的定制模型活页夹,如下所示:Asp.net mvc 2 ASP.NET mvc 2-使用下拉列表绑定出生日期,asp.net-mvc-2,Asp.net Mvc 2,在ASP.NET MVC 2中,如果应用程序必须有3个下拉列表来选择月、日、年,那么如何绑定作为日期时间的视图模型属性呢?我很久以前读过Scott H.关于绑定日期的博客文章,对于这样一个简单的案例来说,这似乎太复杂了。肯定有更干净/更好的方法吗 无论我使用什么解决方案,我都希望使用DataAnnotations之类的东西保留内置验证,并且我还希望能够使用validation属性指定最小/最大日期范围 我首先想到的是一个简单的定制模型活页夹,如下所示: protected override vo
protected override void BindProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, PropertyDescriptor propertyDescriptor)
{
var model = bindingContext.Model as RsvpViewModel;
var form = controllerContext.HttpContext.Request.Form;
if (model == null)
throw new ArgumentException("bindingContext.Model");
if (propertyDescriptor.Name.Equals("BirthDate"))
{
if (!string.IsNullOrEmpty(form["BirthYear"]) &&
!string.IsNullOrEmpty(form["BirthMonth"]) &&
!string.IsNullOrEmpty(form["BirthDay"]))
{
try
{
var yy = int.Parse(form["BirthYear"]);
var mm = int.Parse(form["BirthMonth"]);
var dd = int.Parse(form["BirthDay"]);
model.BirthDate = new DateTime(yy, mm, dd);
return;
}
catch (Exception)
{
model.BirthDate = DateTime.MinValue;
return;
}
}
}
base.BindProperty(controllerContext, bindingContext, propertyDescriptor);
}
然后我尝试创建DateTimeAttribute来进行验证,但在属性声明中指定日期范围时遇到了一些困难,因为属性参数类型是有限的,DateTime不是允许的类型之一
我最终创建了一个IDateRangeProvider接口和一个特定于出生日期的实现,如下所示:
public interface IDateRangeProvider
{
DateTime GetMin();
DateTime GetMax();
}
public class BirthDateRangeProvider : IDateRangeProvider
{
public DateTime GetMin()
{
return DateTime.Now.Date.AddYears(-100);
}
public DateTime GetMax()
{
return DateTime.Now.Date;
}
}
这允许我在视图模型上使用DateTime属性,并保留所有内置属性
[DisplayName("Date of Birth:")]
[Required(ErrorMessage = "Date of birth is required")]
[DateTime(ErrorMessage = "Date of birth is invalid", RangeProvider=typeof(BirthDateRangeProvider))]
public DateTime? BirthDate { get; set; }
但实际上,整个解决方案都有一种过度工程和过度思考的味道。难道没有更好的办法吗
创建seprate 3 Dropdownlist并添加
的必需验证属性
他们
并使用b卫星列表、b月列表
填充你的下拉列表
[DisplayName(“出生日期”)]
公共约会时间?生日{get;set;}
[必需(ErrorMessage=“需要日期”)]
public int BirthDateDate{get;set;}
[必需(ErrorMessage=“需要一个月”)]
public int BirthDateMonth{get;set;}
[必需(ErrorMessage=“需要年份”)]
public int BirthDateYear{get;set;}
公开名单
{
得到
{
//在此处创建列表
}
}
在post方法中,您可以指定
用户选择的模型生日值
BirthDate.Date.AddDays(BirthDateDate-1)。AddMonths(BirthDateMonth)给你一个主意
有班级生日吗
public class birthDate{
public int day{get;set;}
public int month{get;set;}
public int year{get;set;}
}
现在在您的实体中:
将您的生日元素设置为private
并将出生日期项添加到类中
之后,您只需一起处理每个零件的项目。
并将其处理为一个日期:)您可以为生日自定义类型,其属性如下
public class BirthDateModel
{
[Required(), Range(1, 12)]
public Int32 BirthMonth { get; set; }
[Required, Range(1, 31)]
[DayShouldBeValid("BirthYear", "BirthMonth")]
public Int32 BirthDay { get; set; }
[Required,Range(1990, 2012)]
public virtual Int32 BirthYear { get; set; }
public DateTimeOffset GetBirthDate()
{
DateTimeOffset birthDate;
if (DateTimeOffset.TryParse(String.Format("{0}-{1}-{2}", BirthMonth, BirthDay, BirthYear), out birthDate))
return birthDate;
// what should be returned here?
return DateTime.MinValue;
}
}
现在创建自定义验证器aka dayshouldbealdvalid来检查月份和年份,日期是否有效
并非日期的每个部分都有自己的控制。很好的帖子,我一直在想如何做同样的事情,除了通过日期选择器选择日期,然后通过小时和分钟的下拉列表选择时间,然后将其全部绑定到一个Datetime对象
public class BirthDateModel
{
[Required(), Range(1, 12)]
public Int32 BirthMonth { get; set; }
[Required, Range(1, 31)]
[DayShouldBeValid("BirthYear", "BirthMonth")]
public Int32 BirthDay { get; set; }
[Required,Range(1990, 2012)]
public virtual Int32 BirthYear { get; set; }
public DateTimeOffset GetBirthDate()
{
DateTimeOffset birthDate;
if (DateTimeOffset.TryParse(String.Format("{0}-{1}-{2}", BirthMonth, BirthDay, BirthYear), out birthDate))
return birthDate;
// what should be returned here?
return DateTime.MinValue;
}
}