1

我遇到了一个问题,我的控制器将 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!
4

1 回答 1

2

可能是因为您没有在 JS 函数中使用引号传递此参数并将其视为字符串。

buildNotification(@dateString);

应该

buildNotification('@dateString');

您的 JS 函数看到 08/04/2013 并且很可能对这三个整数进行长除法。

于 2013-08-28T18:55:11.430 回答