1

我们使用 JQuery UI Datepicker。

当我以这种格式输入日期时:9/22/2012 asp.net MVC 说它的格式无效。

当我查看通过绑定填充的视图模型时,它表示日期字段:NULL

当我查看请求时,我将其视为发布的值:9%2f22%2f2012

protected override void OnActionExecuting(ActionExecutingContext filterContext))我设置了这样的文化

            CultureInfo culture = new CultureInfo(language.LanguageISO);
            Thread.CurrentThread.CurrentCulture = culture;
            Thread.CurrentThread.CurrentUICulture = culture; 

在这种情况language.LanguageISO下,' en'

我以为模型粘合剂会吸收文化?

日期中的斜线被转义为 可能是一个问题%2f吗?

4

3 回答 3

3

OnActionExecuting事件中设置文化为时已晚。模型绑定器已在此阶段运行。您需要在模型绑定器运行之前提前设置它。例如,您可以实现IAuthorizationFilter接口:

public class SomeCustomAttribute : ActionFilterAttribute, IAuthorizationFilter
{
    public void OnAuthorization(AuthorizationContext filterContext)
    {
        // TODO: set the culture here so that it's picked up by the model binder

        CultureInfo culture = new CultureInfo(language.LanguageISO);
        Thread.CurrentThread.CurrentCulture = culture;
        Thread.CurrentThread.CurrentUICulture = culture; 
    }
}
于 2012-09-17T11:00:41.170 回答
0

尝试像这样配置日期选择器

$('.has_datepicker').datepicker({

       dateFormat: '@System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern.Replace("M", "m").Replace("yy", "y")'
于 2012-09-17T10:58:38.173 回答
0
Follow the step:
1. @Html.TextBox("date", Model.ToString("dd/MM/yyyy"), new { @class = "date" })

2.
/// <reference path="jquery-1.4.4.js" />
/// <reference path="jquery-ui.js" />
$(document).ready(function () {
    $('.date').datepicker({dateFormat: "dd/mm/yy"});
});
于 2012-09-17T11:07:17.927 回答