admin管理员组

文章数量:1390204

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)

but can not get that value in .js file.

following is my .js file code

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);
            },

        });
    }

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)

but can not get that value in .js file.

following is my .js file code

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);
            },

        });
    }
Share Improve this question asked Mar 31, 2014 at 7:55 user3480139user3480139 111 gold badge1 silver badge5 bronze badges 4
  • 1 Are you using AlexChittock / JQuery-Session-Plugin? And What are you getting in data – Satpal Commented Mar 31, 2014 at 7:57
  • Where are you setting the userId? On the client or on the server? If it is at the client, show us the code. – Patrick Hofman Commented Mar 31, 2014 at 7:58
  • @Satpal i am not using any plugin you mentioned. and i am getting return string which controller method returning in data – user3480139 Commented Mar 31, 2014 at 8:39
  • @ Patrick Hofman i am setting userId in Controller – user3480139 Commented Mar 31, 2014 at 8:45
Add a ment  | 

3 Answers 3

Reset to default 2

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

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

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

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

You can use JavaScript variables like this:

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:

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

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

Try the solutions at this link:

Pass Session or Viewbag values to JS Files

本文标签: cHow to get value of session variable using jQuery in external js file in ASPNET MVCStack Overflow