javascript 如何在 ASP.NET MVC 的外部 js 文件中使用 jQuery 获取会话变量的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22756299/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 23:43:21  来源:igfitidea点击:

How to get value of session variable using jQuery in external js file in ASP.NET MVC

c#javascriptjqueryasp.net-mvc

提问by user3480139

I have ASP.NET MVC5 project in which i am setting session value in controller using setSession() which is userId. Then i need to retrieve or get that value in .js file (and set it to TextBox)

我有一个 ASP.NET MVC5 项目,其中我使用 setSession() 在控制器中设置会话值,它是 userId。然后我需要在 .js 文件中检索或获取该值(并将其设置为 TextBox)

but can not get that value in .js file.

但无法在 .js 文件中获取该值。

following is my .js file code

以下是我的 .js 文件代码

function populateUser() {
        debugger;
        $.ajax({
            type: "GET",
            url: '/UserProfile/SetSessionValues',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            async: true,
            success: function (data) {
                if (data) {
                    var user = $.session.get("UserName");
                    $("#txtUserName").val(user);  

                                }
            },
            error: function (msg) {
                alert("Error :" + msg.responseText);
            },

        });
    }

回答by Neel

In your controller :-

在您的控制器中:-

ViewBag.myVar = HttpContext.current.Session["UserName"].ToString();

You can assign Parameters as value of control

您可以将参数分配为控制值

<input type="hidden" value = "@ViewBag.myVar" id="myHiddenVar" /> 

and get it in js file easily

并轻松地将其放入 js 文件中

alert($('#myHiddenVar').val());

回答by Joel

Take a look at RazorJS. You can find it in your Nuget Package Manager.

看看RazorJS。您可以在 Nuget 包管理器中找到它。

It allows you to use razor like tags (ex: @data) in your .jsfiles.

它允许您在文件中使用类似剃刀的标签(例如:@data).js

回答by Umair Hafeez

You can use JavaScript variables like this:

您可以像这样使用 JavaScript 变量:

HTML:

HTML:

<head>
    ...
    <link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />

    @RenderSection("my_script_variables", false)

    <script src="@Url.Content("~/Scripts/external.js")" type="text/javascript"></script>
    ...

</head>

Razor:

剃刀:

@Section my_script_variables {

    <script type="text/javascript">
            var variable1 = '@myVar1',
            variable2 = '@Session["myVar2"]',
            variable3 = '@ViewBag.myVar3';

        </script>

}

External JS:

外部JS:

alert(variable1 + ' ' +variable2 +' '+ variable3);

Also, you can use Hidden Variables as suggested by Neel.

此外,您可以按照 Neel 的建议使用 Hidden Variables。

Try the solutions at this link:

尝试此链接中的解决方案:

Pass Session or Viewbag values to JS Files

将 Session 或 Viewbag 值传递给 JS 文件