I have the following field :
@Html.TextBoxFor(model => model.Patient.dateOfBirth, new { id = "datepicker", @class = "double text white" })
It is a DateTime field that has the following metadata definition:
[Required(ErrorMessage = "Date Of Birth is required")]
[DisplayFormat(DataFormatString = "{0:dd MMM yyyy}")]
public DateTime dateOfBirth;
The issue is that i need it to show the DOB as d/m/y but as a datetime cause i am using a datepicker that needs the value to remain DateTime.
Date picker returns a value that has the dd/mm/yy format. (.ToShortDateString() cant save me this time )
I've tried Darin Dimitrov Answers from here and here , but unless I am a complete moron those don't really work for me.
Later Edit:
I have managed to find the solution for this and wanted to share it with whomever needs it from now on.
@Html.HiddenFor(model => model.DOBirth, new { id = "shortDate", @class = "double text white" })
@Html.TextBox(" ", Model != null ? Model.dateOfBirth.Value.ToShortDateString() : string.Empty, new { id = "datepicker", @class = "double text white" })
And use the following Jquery :
$('#datepicker').datepicker({
changeMonth: true,
changeYear: true,
inline: true,
onSelect: function (dateText, inst) { CalculateAge(dateText); } //an extra method
});
Store the value you get into your hidden field ( via Jquery through the id of the hidden field "shortDate") and thus you can send the value back to the controller