10

I have a ViewBag.IsLocal set to true in controller. I would like to use jquery to check the ViewBag value and display an alert.

Code:

if(@ViewBag.IsLocal == true)
{
alert("yeah");
}

I never get the alert. When I use Firebug to see the value of ViewBag it's True ( with capital T). Do I have to do something like == 'True'? I tried it all and none of that worked.

Thank you for help.

H

3
  • how are you exposing the ViewBag to javascript? Commented Oct 26, 2011 at 15:45
  • I don't think I do. I tried doing something like var isLocal = ViewBag.IsLocal; and then do if(isLocal == true) but that didn't work either Commented Oct 26, 2011 at 15:47
  • You seem to be confusing server side code and client side code. Your controller runs on a webserver, javascript (including jQuery) runs in the browser. Commented Oct 26, 2011 at 15:48

3 Answers 3

21

If you view source on the rendered page, what's being inserted in place of your razor nugget? If IsLocal is a bool type, I think you'll see this:

@if(True == true)
{
  alert("yeah");
}

The reason for that is because true.ToString() is True.

In which case, you'll need to make a string comparison there:

if('@ViewBag.IsLocal' == 'True')
{
  alert("yeah");
}
Sign up to request clarification or add additional context in comments.

14 Comments

Neither of your two code snippets will work. There is no alert function defined in .NET. You need to tell the Razor interpreter to not interpret it as server side code and consider it as literal.
Of course the first one doesn't work. It wasn't supposed to be an example of working code. Did you even read my answer?
ya this answer is more confused than the OP, it further blurs the line between JS and C#
If he's trying to do anything serious with the JavaScript, it would be a disaster trying to use Razor's conditional instead of getting the value into the rendered code and letting JavaScript handle it. You have to blur the line one way or the other; might as well blur it in the right direction.
You are missing the point, and you are still wrong. See Darin's answer for the correct seperation of client/server code.
|
21

Assuming you have set the IsLocal property to a boolean value in your controller action:

public ActionResult Index()
{
    ViewBag.IsLocal = true;
    return View();
}

you could do this on the view:

<script type="text/javascript">
    @if(ViewBag.IsLocal)
    {
        <text>alert("yeah");</text>
    }
</script>

And please don't use ViewBag/ViewData. Use view models and strongly typed views.

So here's a better approach that I prefer. You could JSON serialize your view model into a javascript variable and then deal with it. Like this:

@model MyViewModel

<script type="text/javascript">
    var model = @Html.Raw(Json.Encode(Model));
    // at this stage model is a javascript variable containing
    // your server side view model so you could manipulate it as you wish
    if(model.IsLocal)
    {
        alert("hello " + model.FirstName);
    }
</script>

Obviously if you don't need your entire view model you could JSON serialize only a subset of it => only the part that will be needed by client scripts.

10 Comments

If he's doing anything more complex than an alert() example, mixing server-side Razor conditionals into his JavaScript would be a really bad idea.
@DaveWard, in this case he needs to use a server side variable to include or not a given script. This entire script body could be wrapped in a <text></text> nodes in order to be considered as literal. Inside the script there should be no more mixings. I agree with you that it would be better to externalize the script into a separate javascript file. In this case this <script> tag could be included conditionally using the same syntax as I shown in my answer (wrapping in <text> nodes which have a special meaning in Razor)
You're assuming that he's using that variable to include a script. He didn't say that in the question. What if he needs to test for IsLocal and a client-side condition at some point? Or, do anything more trivial than the if IsLocal then alert(), really? IMO, it doesn't make sense to try to drive that interaction from the server-side, when you can do it from the client-side just as easily (and cleaner, without the <text> clutter) and gain more flexibility.
@DaveWard, it's if('@ViewBag.IsLocal' == 'True') that earned you my downvote. You talk about not mixing server side and client side variables and yet that's what you suggest with it.
I was trying to give the guy a direct answer to his actual question. He was so close, why over-complicate the answer and hide that from him?
|
0

You can use the following function

function parseBoolean(str)
{ 
   return /^true$/i.test(str);
}

and Use it as

if(parseBoolean('@ViewBag.IsLocal') == true) 
{ 
   alert("yeah");
}

2 Comments

Insted of doing crazy HTML to highlight your code, use 4 spaces. Check my edit.
thaks j0k i will consider this in my further Questions and answers.

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.