【发布时间】:2018-09-27 18:00:42
【问题描述】:
我有一个 mvc 控制器,在 Controller 中处理 Session 中的所有 Azure Api 密钥并在视图中传递它。
public ActionResult Index()
{
Session["azureappkey"] = "xxx-xxxx-xxx-xx"; // pass to view
return View();
}
在视图中:
<h2>Index</h2>
<p> @HttpContext.Current.Session["azureappkey"];</p> // got the session value working as expected
@{
var x = HttpContext.Current.Session["azureappkey"]; // not working assign to variable and pass inside the script
<script>
alert(this.x);
alert (x); // GETTING UNDEFINED
</script>
}
Azure 应用密钥写在段落标记中,但未在脚本部分定义。
任何人都可以更正实施。
【问题讨论】:
-
有什么问题吗?什么不工作?
-
我在段落标签或 dom 中获取会话值,当我在变量 x 中分配并在脚本块内传递时,我无法获取值。如何将其存储在变量中并在其他区域内重新使用可见。
-
您正在尝试混合使用 C# 和 JavaScript。一般来说,你不想这样做。
-
当我们使用模型并分配给脚本内的变量时它是如何工作的
-
<script>var x = @Html.Raw(Json.Encode(HttpContext.Current.Session["azureappkey"];)); alert (x); </script>
标签: c# asp.net-mvc asp.net-mvc-4 asp.net-mvc-3 asp.net-mvc-5