在 jquery 中使用 viewbag - asp.net mvc 3

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7905193/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 00:25:36  来源:igfitidea点击:

using viewbag with jquery - asp.net mvc 3

jqueryasp.net-mvc-3viewbag

提问by bobek

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.

我在控制器中将 ViewBag.IsLocal 设置为 true 。我想使用 jquery 检查 ViewBag 值并显示警报。

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.

我从来没有收到警报。当我使用 Firebug 查看 ViewBag 的值时,它是 True(大写 T)。我必须做 == 'True' 之类的事情吗?我尝试了所有方法,但没有一个奏效。

Thank you for help.

谢谢你的帮助。

H

H

回答by Darin Dimitrov

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

假设您已在控制器操作中将 IsLocal 属性设置为布尔值:

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.

并且请不要使用 ViewBag/ViewData。使用视图模型和强类型视图。

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:

所以这里有一个我更喜欢的更好的方法。您可以 JSON 将您的视图模型序列化为一个 javascript 变量,然后对其进行处理。像这样:

@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.

显然,如果你不需要你的整个视图模型,你可以 JSON 序列化它的一个子集 => 只有客户端脚本需要的部分。

回答by Dave Ward

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

如果您在呈现的页面上查看源代码,会插入什么来代替您的剃刀金块?如果IsLocal是 bool 类型,我想你会看到这个:

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

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

原因是因为true.ToString()True

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

在这种情况下,您需要在那里进行字符串比较:

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

回答by ovais

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