由于datetime.now,ModelState.IsValid始终为false

时间:2015-04-25 05:21:20

标签: c# asp.net-mvc datetime kendo-grid

我有一个ASP.NET MVC应用程序,在编辑和创建操作中,我按datetime.now设置了我的类的日期字段。一切正常,我可以添加和编辑记录。但是,当我想要删除这些记录时,ModelStata.IsValid始终为false,错误为"The value '4/25/2015 9:34:39 AM' is not valid for register time."注册时间是我的字段的显示名称。

这是我的行动代码:

public ActionResult Create([DataSourceRequest]DataSourceRequest request, InvtGroups invtGroup)
{
    if (invtGroup != null && ModelState.IsValid)
    {
        invtGroup.DDate = DateTime.Now;
        repo.Insert(invtGroup);
    }
    return Json(new[] { invtGroup }.ToDataSourceResult(request, ModelState));
}
[HttpPost]
public ActionResult Delete([DataSourceRequest]DataSourceRequest request, InvtGroups invtGroup)
{
    if (invtGroup != null && ModelState.IsValid)
        repo.Delete(invtGroup);
    return Json(new[] { invtGroup }.ToDataSourceResult(request, ModelState));
}

这是我的模型(我使用实体框架代码优先):

public class InvtGroups : User
{
    [Key]
    [Column(TypeName = "VARCHAR"), StringLength(21)]
    public string CGroupCode { get; set; }

    [Column(TypeName = "VARCHAR"), StringLength(50)]
    public string CGroupName { get; set; }

    [Column(TypeName = "BIGINT")]
    public Int64? LiCode1 { get; set; }

    [Column(TypeName = "BIGINT")]
    public Int64? LiCode2 { get; set; }

    [Column(TypeName = "BIGINT")]
    public Int64? LiCode3 { get; set; }

    [Column(TypeName = "BIGINT")]
    public Int64? LiCode4 { get; set; }

    [Column(TypeName = "BIGINT")]
    public Int64? LiCode5 { get; set; }
}

用户类:

public class User
{
    [Column(TypeName = "VARCHAR"), StringLength(20)]
    public string CUserNo { get; set; }

    [Column(TypeName = "DATETIME")]
    public DateTime? DDate { get; set; }
}

1 个答案:

答案 0 :(得分:1)

您可以执行以下操作:

1- _Layout.cshtml

 <script>
        kendo.culture("en-GB");
        var culture = kendo.culture();
        culture.calendar.patterns.d = "dd MMM yyyy"; // 25 Feb 2015
        culture.calendar.patterns.D = "dd MMM yyyy";
        culture.calendar.patterns.t = "HH:mm";   // 16:45
        culture.calendar.patterns.T = "HH:mm";
        culture.calendar.patterns.g = "dd MMM yyyy HH:mm";
        culture.calendar.patterns.G = "dd MMM yyyy HH:mm";
 </script>

2- Global.asax

   protected void Application_BeginRequest(object sender, EventArgs e)
        {

            CultureInfo info = new CultureInfo("en-GB");
            info.DateTimeFormat.ShortDatePattern = "dd MMM yyyy";
            info.DateTimeFormat.LongDatePattern = "dd MMM yyyy HH:mm";
            info.NumberFormat.CurrencyDecimalDigits = 2;
            info.NumberFormat.CurrencyGroupSeparator = ",";
            info.NumberFormat.NumberDecimalDigits = 2;
            Thread.CurrentThread.CurrentCulture = info;
            Thread.CurrentThread.CurrentUICulture = info;
        }

上面的代码将设置日期格式以及整个应用程序的数字格式

3-您可以阅读此内容,了解有关如何添加culture js files

的更多信息

希望它会对你有所帮助,如果你还有任何疑问,请继续。