Something really weird is happening when trying to create an EditorTemplate to generate a datepicker. The value passed to the template is a string but is being transformed to Unix timestamp. This string is actually previously transformed from a Unix timestamp on the controller before being sent to the view as a string in my viewModel so I don´t know if both codes are some way linked (??) I have minimized the editor template to:
1 @model String
2
3 @Model.ToString()
4 @Html.TextBox("", Model.ToString())
Line 3 prints the correct value "3/7/14" but Line 4 creates a textbox with value "1404309600"
This is really confusing. Any help? Thank you.
After doing more tests I can explain a bit more what the problem is and where it is coming from. I am using FullCalendar and when I select a range of dates the form to create a new event is opened on a new modal window. The Ajax path request is something like
Create?startDate=1404309600&endDate=1404396000
Then my controller populates the data for the event and amongst other info the form includes the passed start and end dates but as string. All data is passed to the view onto a ViewModel.
The view uses the following line of code to call the EditorTemplate to generate a datepicker input:
@Html.EditorFor(model => model.StartDateVm, new { Value = Model.StartDateVm.ToString() })
And I have decorated the parameters with [UIHint("DatePicker")]
So coming back to the original problem: this line generates an input with the value of the Unix timespan passed to the controller instead the string of the ViewModel.
After some research I realised that the parameter from fullcalendar is called 'startDate' and the one on my ViewModel is 'StartDate'. I have changed the ViewModel attribute name to StartDateVm and all its references to this name and now the input shows the right string. So I guess somehow the TextBox helper is getting the value from the browser instead from the ViewModel.
My question is why this is happening. Now I know a workaround but I would like to understand where the issue is. I think is something to do with this explanation given on MSDN, that I don't completely understand: The value is retrieved in this order - the ModelStateDictionary object, the value of this parameter, the ViewDataDictionary object, and lastly, a value attribute in the html attributes.
So the TextBox on my template was taking the value from a KeyValuePair with the 'StartDate' key that was living on the ModelStateDictionary. This dictionary was populated when my javascript calls my controller and a binding occurs ('Represents the state of an attempt to bind a posted form to an action method'- MDSN). Even I was passing the value of the Model.StartDate to the template the TextBox gives preference to a matching key of the ModelStateDictionary!
So my workaround was giving the starting date of my model the name of StartDateVm. As I found in Gary Clarke post this behaviour of the helpers is helpful so we don't repeat code assigning the same values to a View Model when this already exists on the ModelState. I recommend you Gary´s post.