1

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

        });
    }
4
  • 1
    Are you using AlexChittock / JQuery-Session-Plugin? And What are you getting in data 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. 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 Commented Mar 31, 2014 at 8:39
  • @ Patrick Hofman i am setting userId in Controller Commented Mar 31, 2014 at 8:45

3 Answers 3

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());
Sign up to request clarification or add additional context in comments.

2 Comments

value"@ViewBag.myVar" should be value="@ViewBag.myVar"
@Neel just to test i tried this : Session["UserName"] = "Trupti Rane"; ViewBag.myVar = System.Web.HttpContext.Current.Session["UserName"].ToString(); and then access it from cshtml page using <input readonly id="txtUserName" name="txtUserName" type="text" class="form-control" placeholder="User Name" size="50" style="width: 200px;" value="@ViewBag.myVar"/> but it is not working
0

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.

Comments

-1

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

2 Comments

Although the link might be useful, it is better to include relevant parts of it in your post.
You can still edit your post to include that information. Otherwise it is likely to get deleted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.