1

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!
2
  • ViewData["DateString"] is typed as an object. Is there some disconnect between the implicit conversion between MVC and JavaScript? Would it be enough to just set var dateString = ViewData["DateString"].ToString(); instead? Commented Aug 28, 2013 at 18:51
  • I'm actually handling the conversion to string in the controller before passing it to the view in this case. Commented Aug 28, 2013 at 19:00

1 Answer 1

2

Probably because you are not passing this parameter in your JS function with quotes and treating it like a string.

buildNotification(@dateString);

should be

buildNotification('@dateString');

Your JS function is seeing 08/04/2013 and most likely doing the long division of those three integers.

Sign up to request clarification or add additional context in comments.

3 Comments

Why is it that when the variable is initially stored is does not get stored with the quotes?...I just waisted sooo much time trying to figure this out.
@Stavros_S This is one of the good reasons you have to split your javascript into seperate js files.
@asawyer I hate the fact that I have to put some Javascript in the view, but what is the best way to pass Variables from razor into Javascript with out making the function call from the view

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.