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
2- Global.asax
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.