I'm having an issue where my controller is passing ViewData in to a view, which is then consumed by a javascript function. the ViewData is a date formatted into a string. Yet once passed into Javascript the string turns into '0.021978021978021976' rather than 'mm/dd/yy' fomrat. What would cause this? Here is the code in the controller that is generating this view data.
string dateString;
string cultureConfigKey = System.Configuration.ConfigurationManager.AppSettings["InstanceCulture"];
DateTime? lastEntryDate = null;
DataSet dSet = DataHelper.Measurements_GetLastMeasurement(userSession.UserIDNative);
foreach (DataRow dr in dSet.Tables[0].Rows)
{
lastEntryDate = (DateTime?)dr["When"];
}
// format date based on culture and convert it to string
if (cultureConfigKey == "en" )
{
ViewData["DateString"] = dateString = String.Format("{0:MM/dd/yy}", lastEntryDate);
}
With in the view I reference the ViewData as followed
@
{
var dateString = ViewData["DateString"];
// calling @dateString in this point shows the string in proper format
}
// further down in the view I pass the dateString into the following function which generates some markup that is supposed to prinout out this date string with in it.
buildNotification(@dateString);
// the above function is built in an outside .js file here is its declaration
function buildNotification(dateString){
$('.stats-chartsView').before(
'<div class="noData-popup">' +
'<p class="noData-Img"></p>' +
'<p class="no-data alert">' + CHART_DATA_NOTIFICATION + '<span>'+ dateString +'</span></p>' +
'</div>'
);
}
Thanks!
ViewData["DateString"]is typed as anobject. Is there some disconnect between the implicit conversion between MVC and JavaScript? Would it be enough to just setvar dateString = ViewData["DateString"].ToString();instead?