0

There might be something obvious I'm missing, but I can't seem to make my Javascript evaluate to 'true' and fire the event I want (setting a checkbox to ticked if the value is true/a string equivalent of true).

Code as follows:

window.onload = function () {
        alert("let's go!");
        var enabledVal = TempData["enabled"];

        if (enabledVal == "checked") {
            alert("made it here");
            document.getElementById("eCheck").checked = true;
        }

    }

I know the onload function is being reached because the alert fires. The TempData["enabled"] is set in the controller as follows:

//returns True/False
            string enabledVal = (from person in testTechObj.People
                                 where person.PersonId.Equals(id)
                                 select person.IsEnabled).SingleOrDefault().ToString();

            if (enabledVal == "True")
            {
                enabledVal = "checked";
            }

... 
//pass enabled/authorised values
            TempData["enabled"] = enabledVal;

I tried checking for both a boolean and a string, but the If statement doesn't evaluate in either case. I know TempData["enabled"] is set to what I'd expect (either "checked" or "True") as I tried writing it out to the screen with @TempData["enabled"] and the values I expected appeared.

Is there something obvious I'm missing?

EDIT: Browser output (third bit of text from the top is @TempData["enabled"] [enter image description here]1

3
  • 2
    It looks like you're mixing server-side code and client-side code. What is the resulting client-side code in the browser? It's probably a syntax error and it throwing an error on the browser's debugging console. Commented May 10, 2017 at 19:51
  • 1
    Is your LINQ query really going to return "True" (a string)? Or, will it return True (a Boolean)? Commented May 10, 2017 at 19:53
  • it should return "True" a string given that the conversion is used and the variable is a string itself. Part of the reason I converted 'true' into 'checked' was concern that that was what was tripping the browser up. As for server side, just found this in the debugger: 'Uncaught ReferenceError: TempData is not defined at window.onload' Commented May 10, 2017 at 19:58

1 Answer 1

2

Change TempData to ViewData and onload function to this:

window.onload = function () {
    alert("let's go!");
    var enabledVal = '@ViewData["enabled"]';

    if (enabledVal == "checked") {
        alert("made it here");
        document.getElementById("eCheck").checked = true;
    }

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

4 Comments

The important part: the @ which evaluates that "ViewData" server-side and injects it's value into the javascript.
thanks, that fixed it! it looks as though TempData and ViewData persist differently within the context of views/on load functions
it looks like the ' ' around the ViewData are important too?
yeah, Both of @ and '' are important, @ render your server-side value and '' make a JavaScript string value for you.

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.