如何在Java中比较C#Viewbag中的值? [英] How can I compare a value from C# Viewbag in Javascript?

查看:107
本文介绍了如何在Java中比较C#Viewbag中的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想比较JavaScript中Viewbag中的布尔值.所以起初我尝试了这个:

I want to compare a boolean value from the Viewbag in javascript. So at first I tried this:

if (@Viewbag.MyBoolValue)
    do_sth();

但是随后我在控制台中出现了错误,例如:Value False/True is not declared(不完全是).

But then I have error in console like: Value False/True is not declared (not exactly).

所以我尝试了这个:

@{
    string myBool = ((bool)Viewbag.MyBoolValue) ? "true" : "false";
}

和在javascript中

and in javascript:

if (@myBool == "true")
    do_sth();

但是它也不起作用.我该如何运作?任何帮助将不胜感激.

But it does not work too. How can I make it work? Any help would be appreciated.

推荐答案

假设ViewBag中的值是javascript可以理解的类型,那么您应该使用的内容.

What you have should work, assuming that the value from the ViewBag is of a type which javascript can understand.

但是请注意,您的第一个示例很可能无法正常工作,因为布尔值在javascript中为小写,在C#中为大写.考虑到这一点,请尝试以下操作:

Note however, that your first example most likely did not work because boolean values are lowercase in javascript and uppercase in C#. With that in mind, try this:

var myBoolValue = @ViewBag.MyBoolValue.ToString().ToLower();
if (myBoolValue)
    do_sth();

这篇关于如何在Java中比较C#Viewbag中的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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