-1

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.

EDITED WITH NEW INFO

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.

Mario Lopez
  • 1,405
  • 13
  • 24
  • so what are you asking for? didn't get your question. What do you want? – Krunal Patil Jul 05 '14 at 10:00
  • Why am I getting a -1? and nobody explain to me why my question is bad?! Krunal my question is why this is happening? Is there any reason for the TextBox to format my string to a Unix format? Thanks. – Mario Lopez Jul 05 '14 at 11:37
  • first of all no one said your question is bad, else your question would have gone for hold or removed from the SO. Second thing from above given code no one is able to predict whats going on in your code. Third if you want proper date than try to convert your Unix time into the format you want and pass it to the model. If you want help to convert unix datetime to utc than we can help you. But from the code provided, its impossible for others to find the issue. – Krunal Patil Jul 05 '14 at 11:42
  • Thanks for your explanations. I understand for your answer that there is not reason at all for the TextBox to format my string. I will try to debug the EditorTemplate again. I didn't copy more code because I don't think there is more code involved in this. I will edit the question with more info. – Mario Lopez Jul 05 '14 at 11:46
  • I have included more information. – Mario Lopez Jul 05 '14 at 23:44

1 Answers1

0

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.

Mario Lopez
  • 1,405
  • 13
  • 24