问题
我使用Telerik UI进行ASP.NET MVC网格控制来编辑数据,但返回控制器的日期格式无效且ModelState无效。我正在使用新西兰格式的" dd / MM / yyyy"。所有日期都在网格中正确显示,也可以在内联编辑时显示。在将数据发布回控制器期间似乎发生了这个问题。
我尝试了什么
从stackoverflow上与此问题相关的无数文章中,大多数指向无效的文化设置。但是我在布局文件中为telerik网格设置了文化,见下文。我还在启动文件中为asp.net mvc core设置了全球文化。
错误消息
"价值' 20/10/2010 12:00:00 am。'对于截止日期无效。"
这是我的代码:
Fiddler Raw Post:
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 Raw Response:
{"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%" })))
答案 0 :(得分:0)
经过大量浪费时间后,问题已确定与Windows 10上en-NZ的默认区域设置有关。将区域设置中的日期格式从AM和PM更改为上午和下午。解决了这个问题。这是一个短期的解决方法。