I want to test the data picket on my asp.net mvc web application.
1.So I created the following test model:-
public class TestClass
{
[DataType(DataType.Date)]
public DateTime D { get; set; }
}
2.I added the following script:-
$(function () {
$(".datefield").datepicker();
});
3.And on my view I have the folliwng :-
@model MvcApplication6.Models.TestClass
@Html.EditorFor(item=>item.D)
Now the calender will be displayed well, but I have these two problems:-
1.If inside the action method I specify the data time to equal today date as follow:-
public ActionResult Index()
{
TestClass t = new TestClass();
t.D = DateTime.Now.Date;
return View(t);
}
Then the calender it will switch the day & month, so 12 june become 6 december as follow:-
2.If I remove the dataitme to be equal to today date I will get the following default value:-
01/01/0001
3.If I specify the datetime to allow null, and i do not specify any defualt value I will get the following exception
[DataType(DataType.Date)]
public DateTime? D { get; set; }
The model item passed into the dictionary is null, but this dictionary requires a non-null model item of type 'System.DateTime'.
So can anyone advice on what is causing these three problems ? Thanks
EDIT Ok i changed my property to be String instead of date-time , as follow:-
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
public string CreateDate { get; set; }
But this did not solve the data validation , and still i can not enter date such as 26/06/2014 !!
so i try to remove the [DataType(DataType.Date)]
so my property becomes:-
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
public string CreateDate { get; set; }
but in this way the date picker will not be populated,, since the date picker is defined to get populated on fields of type date,, i think i am missing something , in my understanding to the way things should work ??