简体   繁体   English

如何在 c#asp.net mvc 中从控制器获取会话变量值到 ajax 调用

[英]How to fetch Session variable value from a controller to ajax call in c# asp.net mvc

I want to fetch Session variable value from an mvc controller actionresult which returns a cshtml view.我想从返回 cshtml 视图的 mvc 控制器 actionresult 中获取 Session 变量值。 This ajax call is connected in a button click and is calling from a different cshtml.此 ajax 调用通过按钮单击连接,并从不同的 cshtml 调用。

function accountLogin() {
$.ajax({
    url: accountLogin,
    async: true,
    type: 'POST',
    cache: false,
    success: function (data) {
        var testUser = @Session[Keys.accountrole];
        alert(testUser);
        $("#navigationbar").empty();
        $("#navigationbar").html(data);}

C# Code C# 代码

[HttpPost]
    public ActionResult accountLogin(){
        Session[Keys.accountrole] = "value";
        return View("_viewpage");
    }

The curent implementation is reaturning either undefined session variable or it will display the @session keyword itself not the value in it.当前的实现正在重新返回未定义的会话变量,或者它将显示 @session 关键字本身而不是其中的值。

You can only access Session on the server side.您只能在服务器端访问 Session。 So you have to get a Session variable value inside of the action and retun it back as a json value因此,您必须在操作中获取 Session 变量值并将其作为 json 值重新调回

Try this尝试这个

    [HttpPost]
    public IActionResult accountLogin(){

        //I don't understand why you need it
        Session[Keys.accountrole] = "value";

        var sessionAccountRole=Session[Keys.accountrole];

        return OK( {accountRole=sessionAccountRole} );

        // or if you use an ancient net 
        return Json ( new {accountRole=sessionAccountRole} );

       // or maybe 
   return new JsonResult {accountRole=sessionAccountRole};
    }

and ajax和阿贾克斯

  success: function (data) {
        var testUser = data.accountRole;
        alert(testUser);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在C#中从ASP.NET MVC控制器调用静态方法 - How to call static method from ASP.NET MVC controller in C# 如何使用 ASP.NET MVC C# Html 从视图中调用控制器中的函数 - How to call a function in the controller from the view using ASP.NET MVC C# Html C# & ASP.NET MVC:使用 ajax 调用来调用视图 - C# & ASP.NET MVC : call a view with ajax call 如何将值从javascript变量传递给asp.net mvc中的c#变量 - how to pass a value from javascript variable to c# variable in asp.net mvc Ajax 调用 ASP.NET MVC controller - Ajax Call to ASP.NET MVC controller 如何在ASP.Net MVC中调用控制器方法并从Ajax调用传递对象 - How to call controller method and pass the object from ajax call in ASP.Net MVC 在 C# ASP.NET Core MVC 中使用 AJAX 将数据从视图传递到控制器 - Pass data from view to controller using AJAX in C# ASP.NET Core MVC 如何在C#ASP.Net MVC中为datatable.net ajax调用格式化数据 - How to format data in C# ASP.Net MVC for datatable.net ajax call 如何在提交前使用 Ajax 从 asp.net mvc 登录表单调用控制器方法 - How to call controller method from asp.net mvc Login Form using Ajax before submit ASP.NET MVC 将值从控制器上的变量传递到视图 - ASP.NET MVC passing a value from a variable on a controller to a view
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM