C# asp.net应用中的日期时间验证
因此,我使用razor和C#构建了这个asp.net应用程序,但我无法获得日期-时间的正确验证。C# asp.net应用中的日期时间验证,c#,asp.net,validation,datetime,razor,C#,Asp.net,Validation,Datetime,Razor,因此,我使用razor和C#构建了这个asp.net应用程序,但我无法获得日期-时间的正确验证。 以下是我的应用程序的相关部分 public class EmployeeDto { ... [Required] [DataMember] [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")] // from what I understand th
以下是我的应用程序的相关部分
public class EmployeeDto
{
...
[Required]
[DataMember]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")] // from what I understand this should format the date on the view ackording to the string
public Nullable<DateTime> inDate { get; set; }
...
[Required]
[DataMember]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
public Nullable<DateTime> birthDate { get; set; }
[DataMember]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
public Nullable<DateTime> createdDate { get; set; }
...
}
然而,这似乎也不起作用
如果你有耐心读到最后。你知道解决这种困境的好办法吗?这种行为的问题是,
/
符号被用作分隔符
因此,当您查看计算机上的日期时间时,.NET framework将dd/MM/yyyy
替换为dd-MM-yyyy
。因此,您可以尝试覆盖日期时间分隔符,就像您尝试使用ShortDatePattern
一样,或者您可以在格式字符串中转义/
符号,如下所示:dd'/'MM'/'yyyyy
,但我现在无法尝试
更新: 从 要更改特定日期和时间字符串的日期分隔符,请在文字字符串分隔符中指定分隔符字符。例如,自定义格式字符串
mm'/'dd'/'yyyy
生成一个结果字符串,其中/
始终用作日期分隔符
要更改区域性的所有日期的日期分隔符,请更改当前区域性属性的值,或实例化对象,将字符指定给其属性,并调用包含参数的格式化方法的重载
所以你应该试试这个:
Thread.CurrentThread.CurrentCulture.DateTimeFormatInfo.DateSeparator = '/';
Thread.CurrentThread.CurrentUICulture.DateTimeFormatInfo.DateSeparator = '/';
而@Givan是对的:
验证可能发生在您的服务器上。因此,将使用服务器上指定的日期格式。这也许就是为什么总是使用MM/dd/yyyy
您可以根据
DisplayFormatAttribute
public class DateFormatBinding : DefaultModelBinder
{
public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
string displayFormat = bindingContext.ModelMetadata.DisplayFormatString;
ValueProviderResult 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;
}
bindingContext.ModelState.AddModelError(bindingContext.ModelName, string.Format("{0} is an invalid date format", value.AttemptedValue));
}
return base.BindModel(controllerContext, bindingContext);
}
}
将其添加到project中,然后在Global.asax中注册
protected void Application_Start()
{
...
ModelBinders.Binders.Add(typeof(DateTime), new DateFormatBinding());
ModelBinders.Binders.Add(typeof(DateTime?), new DateFormatBinding());
...
}
我猜你在跑IIS。如果是这样,请查看以下内容:
也许这也有帮助。
您的意思是-客户端验证?作为一种解决方法-您可以尝试使用正则表达式验证,而不是在您的服务器上进行验证。因此,将使用服务器上指定的日期格式。这也许就是为什么总是使用MM/dd/yyyy的原因。@Giwan,用于测试我的开发PC也是服务器。尚未部署应用程序。这是解决方案的一部分。我成功地在其构造函数中为控制器线程设置了区域性:(Global.asax中的代码似乎没有达到预期效果。验证现在对UI上显示的日期非常有效,但隐藏日期的字符串仍然由“yyyy-MM-dd”中的视图返回格式。并以我的自定义格式进行验证。我最终决定将@Html.HiddenFor(model=>model.createdDate)
替换为隐藏的div@Html.Label(“创建日期:”)@Html.EditorFor(model=>model.createdDate)@Html.ValidationMessageFor(model=>model.createdDate)
一切正常
public class DateFormatBinding : DefaultModelBinder
{
public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
string displayFormat = bindingContext.ModelMetadata.DisplayFormatString;
ValueProviderResult 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;
}
bindingContext.ModelState.AddModelError(bindingContext.ModelName, string.Format("{0} is an invalid date format", value.AttemptedValue));
}
return base.BindModel(controllerContext, bindingContext);
}
}
protected void Application_Start()
{
...
ModelBinders.Binders.Add(typeof(DateTime), new DateFormatBinding());
ModelBinders.Binders.Add(typeof(DateTime?), new DateFormatBinding());
...
}