ModelState.IsValid is always false because of date

2019-07-22 04:34发布

问题:

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

回答1:

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.