如何使用 MVC4 和 Razor 设置 javascript 变量 [英] How to set javascript variables using MVC4 with Razor

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

问题描述

有人可以格式化下面的代码,以便我可以使用 razor 用 c# 代码设置 srcript 变量吗?

Can someone format the code below so that I can set srcript variables with c# code using razor?

以下不起作用,我已经这样做了,让别人可以轻松提供帮助.

The below does not work, i've got it that way to make is easy for someone to help.

@{int proID = 123; int nonProID = 456;}

<script type="text/javascript">
    @{

     <text>  

    var nonID =@nonProID;
    var proID= @proID;
    window.nonID = @nonProID;
    window.proID=@proID;

    </text>
}
</script>

我收到一个设计时错误

推荐答案

您应该查看生成的 razor 页面的输出.实际上,您需要知道server-sideclient-side 执行了什么.试试这个:

You should take a look at the output that your razor page is resulting. Actually, you need to know what is executed by server-side and client-side. Try this:

@{
    int proID = 123; 
    int nonProID = 456;
}

<script>

    var nonID = @nonProID;
    var proID = @proID;
    window.nonID = @nonProID;
    window.proID = @proID;

</script>

输出应该是这样的:

根据您使用的 Visual Studio 版本,它指出了带有 razor 的视图在设计时的一些亮点.

Depending what version of Visual Studio you are using, it point some highlights in the design-time for views with razor.

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

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