[英]Update Razor variable from within a javascript function
I am a noob in using Razor and now I am trying to update a Razor variable from within a javascript function.我是使用 Razor 的菜鸟,现在我正在尝试从 javascript 函数中更新 Razor 变量。 For example:例如:
@{
bool myVariable = false;
}
<script type="text/javascript">
function Foo() {
if (some_condition)
{
@myVariable = true;
}
}
</script>
@if (myVariable)
{
<div>
<!-- stuff -->
</div>
}
Is that possible?那可能吗? If so, how can I do it?如果是这样,我该怎么做?
@myVariable
is a server-side variable it get's it's value from server before rendering the page @myVariable
是一个服务器端变量,它在呈现页面之前从服务器获取它的值
@myVariable
will print the value of the variable not assign to it, @myVariable
将打印未分配给它的变量的值,
so the output of @myVariable = true;
所以@myVariable = true;
的输出@myVariable = true;
will be将会
false = true;
use ajax to get content from server使用ajax从服务器获取内容
@{
bool myVariable = false;
}
<script type="text/javascript">
function Foo() {
if (some_condition)
{
$('#newContent').load('/News/GetLatest/10'); // call ajax to get content
}
}
</script>
<div id="newContent">
<!-- stuff -->
</div>
or you can only show the div if condition is true on the client side或者您只能在客户端条件为真时显示 div
@{
bool myVariable = false;
}
<script type="text/javascript">
var showContent = @myVariable; // false
function Foo() {
if (some_condition)
{
showContent = true;
$('#newContent').show(); // show it
}
}
</script>
<div id="newContent" style="display: none;"> <!-- hidden by default -->
<!-- stuff -->
</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.