使用viewbag使用jQuery - asp.net MVC 3 [英] using viewbag with jquery - asp.net mvc 3

查看:128
本文介绍了使用viewbag使用jQuery - asp.net MVC 3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ViewBag.IsLocal设置为true控制器。我想使用jQuery检查ViewBag值,并显示一个警告。

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");
}

我从来没有得到警报。当我使用Firebug看到ViewBag的值是真的(资本T)。我必须做的是这样=='真'?我想这一切,都不奏效。

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.

感谢您的帮助。

^ h

推荐答案

如果您所呈现的页面上查看源代码,什么是被插入的地方你的剃须刀金块的?如果 IsLocal 是一个布尔类型,我想你会看到这一点:

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");
}

这其中的原因是因为 true.ToString()

在这种情况下,你需要做一个字符串比较有:

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

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

这篇关于使用viewbag使用jQuery - asp.net MVC 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆