在 Razor 中使用 javascript 变量 [英] Using a javascript variable within Razor

查看:109
本文介绍了在 Razor 中使用 javascript 变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道 Razor 是服务器端和 Javascript 客户端.我正在尝试解决此问题.这是我想要的:

I am aware that Razor is server side and Javascript client side. I am trying to workaround this. Here's what I wanted:

剃刀:

if (count > 0)
{
    t.Add().Text("Yeah")
         ...
         ...
    }
}

但该计数是在视图的开头定义的,如下所示:

But that count is defined at the beginning of the View, like this:

@{
    int count = 2;
}

但我不能这样定义它:count 的值在一个 javascript 变量上,设置在 document.ready 上.然后,我想在 Razor if 条件中使用它.

But I can't define it like that: the value for count is on a javascript variable, set on document.ready. Then, I want to use it in the Razor if condition.

是否可以解决此问题?

推荐答案

Simply No ,原因是相同的服务器端语言先运行,而不是浏览器运行客户端语言后,所以你可以使用客户端语言中的服务器端变量值,您可以从服务器端生成客户端代码,但反之亦然,

Simply No , The reason is same server-side language run first, than after browser run client side languages, So you can use server side variables values in client side language, you can generate client side code from server side but this all will not work in vice-versa,

您对问题的选择只是Ajax 请求

The option you have for your problem is only Ajax request

这篇关于在 Razor 中使用 javascript 变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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