ModelState.IsValid总是因为datetime.now假 [英] ModelState.IsValid is always false because of datetime.now

查看:196
本文介绍了ModelState.IsValid总是因为datetime.now假的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ASP.NET MVC应用程序并在编辑和创建操作设置我的班日期字段通过 datetime.now 。一切工作正常,我可以添加和编辑记录。但是,当我想删除这些记录 ModelStata.IsValid 总是和误差 值2015年4月25日上午09时34分39秒是无效的注册时间。注册时间是我场的显示名称。

下面是我的行为code:

 公众的ActionResult创建([DataSourceRequest] DataSourceRequest要求,InvtGroups invtGroup)
{
    如果(invtGroup = NULL&放大器;!&安培; ModelState.IsValid)
    {
        invtGroup.DDate = DateTime.Now;
        repo.Insert(invtGroup);
    }
    返回JSON(新[] {} invtGroup .ToDataSourceResult(请求的ModelState));
}
[HttpPost]
公众的ActionResult删除([DataSourceRequest] DataSourceRequest要求,InvtGroups invtGroup)
{
    如果(invtGroup = NULL&放大器;!&安培; ModelState.IsValid)
        repo.Delete(invtGroup);
    返回JSON(新[] {} invtGroup .ToDataSourceResult(请求的ModelState));
}

这是我的模型(我使用实体框架code在前):

 公共类InvtGroups:用户
{
    [键]
    [专栏(类型名=VARCHAR),StringLength(21)]
    公共字符串CGROUP code {搞定;组; }    [专栏(类型名=VARCHAR),StringLength(50)]
    公共字符串CGroupName {搞定;组; }    [专栏(类型名=BIGINT)]
    公众的Int64?李code1 {搞定;组; }    [专栏(类型名=BIGINT)]
    公众的Int64?李$ C $ {C2获得;组; }    [专栏(类型名=BIGINT)]
    公众的Int64?李$ C $ {C3获得;组; }    [专栏(类型名=BIGINT)]
    公众的Int64?李$ C $ {C4获得;组; }    [专栏(类型名=BIGINT)]
    公众的Int64?李$ C $ {C5获得;组; }
}

和用户类:

 公共类用户
{
    [专栏(类型名=VARCHAR),StringLength(20)]
    公共字符串CUserNo {搞定;组; }    [专栏(类型名=DATETIME)]
    公众的DateTime? DDate {搞定;组; }
}


解决方案

您可以执行以下操作:

1 _Layout.cshtml

 <脚本>
        kendo.culture(EN-GB);
        VAR文化= kendo.culture();
        culture.calendar.patterns.d =DD MMM YYYY; // 2015年2月25日
        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中

 保护无效的Application_BeginRequest(对象发件人,EventArgs的发送)
        {            CultureInfo的信息=新的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 =信息;
            Thread.CurrentThread.CurrentUICulture =信息;
        }

以上code将建立日期格式,格式也为您的整个应用程序的数量。

3,你可以阅读有关如何添加文化JS更多信息文件

希望这会帮助你,如果你还有任何疑问,请便。

I have an ASP.NET MVC application and in the edit and create actions I set date field of my class by datetime.now. Everything works fine and I can add and edit records. But when I want to delete those records ModelStata.IsValid is always false and the error is "The value '4/25/2015 9:34:39 AM' is not valid for register time." Register time is display name of my field.

Here is my actions code:

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));
}

This is my Model (I'm using Entity Framework Code First):

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; }
}

And the user class:

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

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

解决方案

You can do the following:

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;
        }

the above code will setup the date format and also the number formatting for your whole application

3- you can read this for more information about how to add the culture js files

hope it will help you and if you still have any question, go ahead.

这篇关于ModelState.IsValid总是因为datetime.now假的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆