我得到了答案我使用的自定义ModelBinder的,为了解决这个问题,
首先,我注册的这条线在Application_Start方法在Global.asax中:
ModelBinders.Binders.Add(typeof(DateTime?), new MyDateTimeModelBinder());
这里是自定义模型绑定器:
public class MyDateTimeModelBinder : DefaultModelBinder
{
public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
var displayFormat = bindingContext.ModelMetadata.DisplayFormatString;
var value = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
if (!string.IsNullOrEmpty(displayFormat) && value != null)
{
DateTime date;
displayFormat = displayFormat.Replace("{0:", string.Empty).Replace("}", string.Empty);
// use the format specified in the DisplayFormat attribute to parse the date
if (DateTime.TryParseExact(value.AttemptedValue, displayFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out date))
{
return date;
}
else
{
bindingContext.ModelState.AddModelError(
bindingContext.ModelName,
string.Format("{0} is an invalid date format", value.AttemptedValue)
);
}
}
return base.BindModel(controllerContext, bindingContext);
}
}
感谢Darin Dimitrov的answer!
尝试使用数据类型:[DataType(DataType.Date)] – crunchy
请创建一个[MCVE](http://stackoverflow.com/help/mcve)。 –
假设你的服务器文化是接受'dd/MM/yyyy'中的日期的文化,那么问题是'jquery.validate',它验证'MM/dd/yyyy'格式的日期。你还没有表明,如果你使用日期选择器,但参考[这个答案](http://stackoverflow.com/questions/27285458/jquery-ui-date-picker-and-mvc-view-model-type-datetime/27286969#27286969 )一些选项 –