如何在JavaScript中创建MVC3剃刀视图引擎.cshtml会话变量 [英] How to Create Session Variable in JavaScript MVC3 Razor View engine .cshtml

查看:141
本文介绍了如何在JavaScript中创建MVC3剃刀视图引擎.cshtml会话变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用MVC3剃刀视图引擎,

I am using MVC3 with Razor View engine,

我有.cshtml网页中,我有一个JavaScript函数,即JavaScript函数里面,我想创建Session变量和检索相同的JavaScript函数会话。

i have .cshtml page in that i have a JavaScript function, inside that JavaScript function, i want to create Session variable and retrieve that session in same JavaScript function.

如何实现这一目标。

推荐答案

Session是在服务器端,所以你需要调用服务器,以设置或检索会话变量。

Description

The Session is on the server side so you need to call the server in order to set or retrieve session variables.

刚刚张贴到控制器,并设置会话变量在那里。

Just post to a controller and set the Session variable there.

的jQuery

$(function () {
    $.post('/SetSession/SetVariable', 
           { key : "TestKey", value : 'Test' }, function (data) 
    {
        alert("Success " + data.success);
    });
});

的mvc控制器

public class SetSessionController : Controller
{
    public ActionResult SetVariable(string key, string value)
    {
        Session[key] = value;

        return this.Json(new { success = true });
    }
}

更多信息


  • Save并在MVC 3应用程序
  • 使用JQuery通过Ajax检索会话数据

    More Information

    • Save and retrieve Session data via Ajax using JQuery in an MVC 3 application
    • 这篇关于如何在JavaScript中创建MVC3剃刀视图引擎.cshtml会话变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆