我遇到了一个问题,我的控制器将 ViewData 传递给视图,然后由 javascript 函数使用。ViewData 是格式化为字符串的日期。然而,一旦传入 Javascript,字符串就会变成 '0.021978021978021976' 而不是 'mm/dd/yy' 格式。什么会导致这个?这是生成此视图数据的控制器中的代码。
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);
}
在视图中,我引用 ViewData 如下
@
{
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!