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
3 Answers
Reset to default 2In 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
版权声明:本文标题:c# - How to get value of session variable using jQuery in external js file in ASP.NET MVC - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744747854a2622995.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论