从 mvc 控制器设置一个 javascript 变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13294914/
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
Set a javascript variable from mvc controller
提问by Adam
Is it possible to set a javascript variable from a c# controller? We have a situation where we override our master page with a dumb downed version that doesn't require login for users. However, our javascript timeout timer still runs. I would like to in the controller method that overrides the master, to override the timeout to something huge.
是否可以从 ac# 控制器设置 javascript 变量?我们有一种情况,我们使用不需要用户登录的愚蠢版本覆盖我们的母版页。但是,我们的 javascript 超时计时器仍在运行。我想在覆盖 master 的控制器方法中,将超时覆盖为巨大的东西。
public dumbDownController()
{
ViewData["MasterPageOverride"] = "~/Views/Shared/_NoLogin.cshtml";
//Somehow reset that timer below from 20 to like 9999. To simulate no timeout.
return View("Cities", model);
}
Then our javascript file has.
然后我们的javascript文件就有了。
tb.sessionTimer = (function () {
var SESSION_TIMEOUT = 20;
var currentTimeoutCounter = SESSION_TIMEOUT * 60;
...
lots more irrelevant to question
...
}
Large app, so looking to barely change the javascript. Would like to handle it from the controller.
大型应用程序,因此希望几乎不更改 javascript。想从控制器处理它。
回答by SoonDead
Short Answer:
简答:
<script>
var xyz = @ViewData["MasterPageOverride"];
</script>
Longer Answer:
更长的答案:
You can't directly assign JavaScript variables in the Controller, but in the View you can output JavaScript which can do the same.
您不能直接在控制器中分配 JavaScript 变量,但在视图中您可以输出可以执行相同操作的 JavaScript。
You need to pass the variable to the View somehow. For this example I'll use the ViewData
object dictionary. You can set an element in the Controller:
您需要以某种方式将变量传递给 View。对于这个例子,我将使用ViewData
对象字典。您可以在控制器中设置一个元素:
public ActionResult SomeAction()
{
ViewData["aNumber"] = 24;
}
Then in the View it is possible to use it as:
然后在视图中可以将其用作:
<script>
var xyz = @ViewData["aNumber"];
</script>
Which will be sent to the client browser as:
它将作为以下内容发送到客户端浏览器:
<script>
var xyz = 24;
</script>
Stringsneed a bit more attention, since you need to enclose them between ''
or ""
:
字符串需要多加注意,因为您需要将它们括在''
或之间""
:
<script>
var xyz = "@ViewData["aString"]";
</script>
And as @Graham mentioned in the comments, if the string happens to be valid JSON(or any object literal, but it is very easy to create JSON), and you want to use it as a JavaScript object, you can:
正如@Graham 在评论中提到的,如果字符串恰好是有效的JSON(或任何对象文字,但创建 JSON 非常容易),并且您想将其用作 JavaScript 对象,您可以:
<script>
var xyz = @ViewData["aJsonString"];
</script>
Just make sure the output is valid JavaScript.
只要确保输出是有效的 JavaScript。
On a footnote, be careful with special HTML characters, like <
as they are automatically HTML-encodedto prevent XSS attacks, and since you are outputting JavaScript not HTML, this can mess things up. To prevent this, you can use Html.Raw()
like:
在脚注中,请注意特殊的 HTML 字符,<
因为它们会自动进行HTML 编码以防止 XSS 攻击,并且由于您输出的是 JavaScript 而不是 HTML,这可能会造成混乱。为了防止这种情况,您可以使用Html.Raw()
类似:
<script>
var xyz = @Html.Raw(ViewData["aJsonString"]);
</script>
回答by Madbreaks
If tb
is within the scope of your controller, consider just overriding the sessionTimer function:
如果tb
在您的控制器范围内,请考虑覆盖 sessionTimer 函数:
public dumbDownController(){
...
tb.sessionTimer = function(){return false;}
}
回答by user1477388
You have two options (as I understand):
你有两个选择(据我所知):
Create the variable from viewbag, data, tempdata, like so:
从 viewbag、data、tempdata 创建变量,如下所示:
var SESSION_TIMEOUT = @ViewData["MasterPageOverride"];
var SESSION_TIMEOUT = @ViewBag["MasterPageOverride"];
var SESSION_TIMEOUT = @TempData["MasterPageOverride"];
Or, do it via jQuery AJAX:
或者,通过 jQuery AJAX 来实现:
$.ajax({
url: '/YourController/YourAction',
type: 'post',
data: { id: id },
dataType: 'json',
success: function (result) {
// setVar
}
});