从控件返回到控制器的日期格式无效

本文关键字:日期 格式 无效 控制器 控件 返回 | 更新日期: 2023-09-27 17:59:21

问题

我正在使用ASP.NET MVC网格控件的Telerik UI来编辑数据,但是返回到控制器的日期格式无效,ModelState无效。我使用的是新西兰格式"dd/MM/yyyy"。所有日期都会在网格中正确显示,并且在内联编辑时也会正确显示。问题似乎发生在将数据发送回控制器的过程中。

我尝试过的

从stackoverflow上无数篇与这个问题相关的文章中,大多数都指向无效的区域性设置。然而,我已经在布局文件中设置了telerik网格的文化,见下文。我还在启动文件中设置了asp.net mvc核心的全局文化。

错误消息

"值"20/10/2010 12:00:00 a.m."对到期日无效。"

这是我的代码:

  • ASP.NET核心剑道
  • ASP.NET MVC的用户界面

小提琴手原始帖子:

sort=&group=&filter=&InvoiceID=237&CompanyID=15&Description=Monthlyy&InvoiceDate=30%2F09%2F2010+12%3A00%3A00+a.m.&DueDate=20%2F10%2F2010+12%3A00%3A00+a.m.&Tax=0&Discount=0&Paid=true&Company.CompanyID=15&Company.Name=Engineering&Company.Address=&Company.Phone=&Company.Fax=&Company.CountryID=&Company.Comment=&InvoiceItems=

Fiddler原始响应:

{"Data":[{"InvoiceID":237,"CompanyID":15,"Description":"Monthlyy","InvoiceDate":null,"DueDate":null,"Tax":0.0,"Discount":0,"Paid":true,"Company":{"CompanyID":15,"Name":"Engineering","Address":null,"Phone":null,"Fax":null,"CountryID":null,"Comment":null},"InvoiceItems":[]}],"Total":1,"AggregateResults":null,"Errors":{"DueDate":{"errors":["The value '20/10/2010 12:00:00 a.m.' is not valid for Due Date."]},"InvoiceDate":{"errors":["The value '30/09/2010 12:00:00 a.m.' is not valid for Invoice Date."]}}}

我的视图:

@(Html.Kendo().Grid<GearBox.Models.Invoice>()
    .Name("grid")
    .Columns(columns =>
    {
        columns.Bound(p => p.Description);
        columns.Bound(p => p.Company.Name).Filterable(ftb => ftb.Multi(true).Search(true));
        columns.Bound(p => p.InvoiceDate).Width(130);
        columns.Bound(p => p.DueDate).Width(130);
        columns.Bound(p => p.Paid).Width(80).ClientTemplate("<input type='checkbox' disabled='disabled' #= Paid ? checked='checked' :'' # />");
        columns.Command(command => { command.Edit(); command.Destroy(); }).Width(200);
    })
    .ToolBar(toolbar => toolbar.Create())
    .Pageable(pageable => pageable
       .Input(true)
       .Numeric(false)
     )
    .Sortable()
    .Scrollable()
    .HtmlAttributes(new { style = "height:430px;" })
    .DataSource(dataSource => dataSource
        .Ajax()
        .PageSize(20)
        .Model(model => model.Id(p => p.InvoiceID))
        .Events(events => events.Error("onError"))        
        .Create(update => update.Action("Invoices_Create", "Invoice"))
        .Read(read => read.Action("Invoices_Read", "Invoice"))
        .Update(update => update.Action("Invoices_Update", "Invoice"))
        .Destroy(update => update.Action("Invoices_Destroy", "Invoice"))
    )
    .Editable(editable => editable.Mode(GridEditMode.PopUp))
)

启动

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    ...
    app.UseRequestLocalization(BuildLocalizationOptions());
    ...
    app.UseKendo(env);
}
private RequestLocalizationOptions BuildLocalizationOptions()
{
    var supportedCultures = new List<CultureInfo>
    {
        new CultureInfo("en-NZ")          
    };
    var options = new RequestLocalizationOptions
    {
        DefaultRequestCulture = new RequestCulture("en-NZ"),
        SupportedCultures = supportedCultures,
        SupportedUICultures = supportedCultures
    };
    return options;
}

布局文件的标题部分

<head>
       ... 
        @{              
            var culture = System.Globalization.CultureInfo.CurrentCulture.ToString();
        }
        <script src="@("https://kendo.cdn.telerik.com/2016.2.607/js/cultures/kendo.culture." + culture + ".min.js")"></script>
        <script>
            //Set the current culture
            kendo.culture("@culture");
        </script>
        ....
</head>

型号

[ScaffoldColumn(false)]
    [Display(Name = "ID")]
    [DataType("Integer")]
    //[Range(0, int.MaxValue)]
    public int InvoiceID { get; set; }
    [Required]
    public int CompanyID { get; set; }
    [Required]
    [MaxLength(15)]      
    public string Description { get; set; }
    [Display(Name = "Invoice Date")]
    [DataType(DataType.Date)]
    [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}",
           ApplyFormatInEditMode = false)]
    public DateTime? InvoiceDate { get; set; }
    [Display(Name = "Due Date")]
    [DataType(DataType.Date)]
    [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}",
           ApplyFormatInEditMode = false)]
    public DateTime? DueDate { get; set; }

编辑器模板

@model DateTime?
@(Html.Kendo().DatePickerFor(m => m).HtmlAttributes(new { style = "width:80%" })))

从控件返回到控制器的日期格式无效

经过大量的浪费时间,该问题已被确定与windows 10上en NZ的默认区域设置有关。将区域设置中的日期格式从上午和下午更改为上午和下午解决了此问题。这是一个短期的解决方法。