1

I'm working on a MVC3 app. In my action, I have ViewBag object that has either true or false value - I used breakpoint and can see that in the action, they have the correct values. Now, when I get to the view, I want to check these ViewBag object in jQuery Code:

if (@ViewBag.ind == true) {
        $objKpr = $("[email protected]");
        $objKpr.click();
    }

When I tested own code with Firebug, it returned

 if (False == false) 
         {
        $objKpr = $("[email protected]");
        $objKpr.click();
         }

Viewbag was interpreted as a 'False' rather than a variable holding a value of false.Why is this happening?

Thanks for answers But the question was in another-Why a ViewBag inside a jQuery would not yield the correct value?

3 Answers 3

1

You can simply use

var condition = @(myBooleanVariableInCSharp ? "true" : "false";
if (condition == true) {
    $objKpr = $("[email protected]");
    $objKpr.click();
}

OR

You should use @ViewBag.ind.ToString().ToLower(). You need to convert the value which JavaScript can understand.

Your problem as bool values are lowercase in javascript and uppercase in C#

Like

if (@ViewBag.ind.ToString().ToLower() == true) {
    $objKpr = $("[email protected]");
    $objKpr.click();
}
Sign up to request clarification or add additional context in comments.

4 Comments

+1 can't see anything wrong with this answer, would also love to know why someone downvoted it?
Satpal.Why is @ViewBag.ind.ToString().ToLower()?why not just use ViewBag.ind.ToLower()?
This solution may cause errors if @ViewBag.ind is null. BTW, i'm not the downvoter.
@Denis @ViewBag.ind is assumed to be of type bool, so the ToLower() method isn't available on it without turning it into a string first.
1

Calling @ViewBag.MyProperty in your code will cause the Razor engine to render the property by calling ToString() on the value. E.g.

if (@ViewBag.ind == true) {

Renders as:

if (True == true) {

So you may want to change your code to something like:

if (@ViewBag.ind.ToString().ToLowerInvariant() === true)

Although the values in ViewBag are only available to inject into the JavaScript/View when the page is rendered. They're not as dynamic as JavaScript, where the variables are held client side and can be flipped/changed as many times as you like.

You may wish to use the technique to inject initial values for your scripts:

$(document).ready(function () {
    var ind = @ViewBag.ind.ToString().ToLowerInvariant();

    DoSomething(ind);
});

function DoSomething(ind) {
    if (ind) {
        $objKpr = $("[email protected]");
        $objKpr.click();
    }
}

This way you can initialise the behaviour of ind at render, but then change the value of ind on the client side and call the function DoSomething() many times with different values.

You may wish to look into using JSON, AJAX calls and the JavaScript Module pattern.

Comments

0

Try this;

if ("@(ViewBag.ind == null ? "false" : ViewBag.ind.ToString().ToLower())" === "true") {

It will not give errors even if ViewBag.ind is null.

Thanks!

Comments

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.