简体   繁体   English

如何从ASP.NET MVC用JavaScript获取会话

[英]how to get Session in javascript from ASP.NET MVC

I have some belows , please help me how i can get Session in Javascript code from Controller ? 我有一些下面的内容,请帮助我如何从Controller获取Java代码中的Session?

public ActionResult Login(FormCollection f)
    {
        string sAcount = f["txtAccount"].ToString();
        string sPassword = f.Get("txtPassword").ToString();

        tblCustom cs = db.tblCustoms.SingleOrDefault(n=>n.Account==sAccount && n.Password==sPassword);

        if (cs != null)
        {

            Session["Account"] = cs;

            return View();

        }

        return View();

    }

and JS code is 和JS代码是

        <script > 

       $('#btnSendMsg').click(function () {

                  var msg = $("#txtMessage").val();

                  alert('Hello' + Session["Account"] );
                 });   

    <script/>

the result is alert stil is not working, help me. 结果是警报stil无法正常工作,请帮助我。

Although this doesn't directly answer your question, the preferred approach is to create ViewModels while passing and retrieving parameters. 尽管这不能直接回答您的问题,但是首选的方法是在传递和检索参数时创建ViewModel。

Create a LoginViewModel: 创建一个LoginViewModel:

public class LoginViewModel {
   public tblCustoms Customs { get; set; }
   //other stuff you have, you might consider moving account and password here too, 
   //instead of capturing with textbox names

   //public string Account { get; set; }
   //public string Password { get; set }
}

Pass that instead to the view. 将其传递给视图。

public ActionResult Login(FormCollection f)
{
    string sAcount = f["txtAccount"].ToString();
    string sPassword = f.Get("txtPassword").ToString();

    var cs = db.tblCustoms.SingleOrDefault(n=>n.Account==sAccount && n.Password==sPassword);

    if (cs != null)
    {
        Session["Account"] = cs;

        //return View(); you don't need this line
    }

    return View(new LoginViewModel() { Customs = cs });

}

Add to top of your view: 添加到您的视图顶部:

@model YourNameSpace.LoginViewModel

And in the javascript: 在javascript中:

<script>
   $('#btnSendMsg').click(function () {
        var msg = $("#txtMessage").val();
        alert('Hello ' + @Model.Customs );
    });
<script/>

As an alternative to all of these, you can use ViewBag . 作为所有这些的替代,您可以使用ViewBag In the controller method, assign it to any name: 在控制器方法中,将其分配给任何名称:

ViewBag.Customs = cs;

Then call it in the view: 然后在视图中调用它:

alert('Hello ' + @ViewBag.Customs );

In order to use Session in your view, try this: 为了在您的视图中使用会话,请尝试以下操作:

@Session["Account"].ToString();

You should not update sessions many times, the type of data stored in Sessions are User Roles, Page Permissions and other global information. 您不应该多次更新会话,会话中存储的数据类型为用户角色,页面权限和其他全局信息。 Once the login is done you should set login cookie. 登录完成后,您应该设置登录cookie。 For login, you should use FormsAuthentication cookie. 对于登录,您应该使用FormsAuthentication cookie。

Follow set Forms authentication to set forms authentication cookie. 按照set Forms身份验证设置表单身份验证cookie。 Or check this link Create Forms Authentication cookie . 或检查此链接创建表单身份验证cookie

In the page use 在页面中使用

alert("@HttpContext.Current.User.Identity.Name");

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

相关问题 如何从JavaScript ASP.NET中的会话获取对象列表? - How to get list of objects from session in javascript ASP.NET? 如何从 javascript 数组中获取项目到 asp.net session - How to get items from javascript array to asp.net session 如何在ASP.NET MVC中从View到JavaScript获取值 - How to get a value from View to JavaScript in asp.net MVC 如何在asp.net MVC中的外部JavaScript文件中获取会话值,tempdata? - How to get the session value, tempdata in external JavaScript file in asp.net MVC? 从asp.net获取会话值到javascript - get session value from asp.net to javascript 如何从javascript,asp.net mvc3中设置的cookie获取密钥值 - How to get key Value from cookies which was set in javascript, asp.net mvc3 如何使用JavaScript从ASP.Net MVC中的表单获取“验证消息”的值? - How can I get the value of Validation Message from form in ASP.Net MVC, using javascript? 如何从javascript文件到ASP.NET MVC视图获取函数的值? - How to get a value of a function from a javascript file to an ASP.NET MVC view? 如何使用 JavaScript 从 Html.BeginForm() ASP.NET MVC 获取值 - How to get values using JavaScript from Html.BeginForm() ASP.NET MVC 如何使用ASP.NET MVC中的Javascript打开窗口? - How to open a window using Javascript from ASP.NET MVC?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM