如何获取在控制器中创建的cshtml页面中的javascript中的cookie [英] How to get the cookie in javascript in cshtml page which was created in controller

查看:725
本文介绍了如何获取在控制器中创建的cshtml页面中的javascript中的cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户退出并希望在登录页面中获取该用户登录ID以填充登录ID文本框时,我已创建了具有用户登录ID的Cookie。



但我没有在登录页面的javascript函数中获取cookie值。



我的注销 c> homecontroller 是这样的:

  public ActionResult Logout()
{
string loginId = SessionManager.UserloginId;
FormsAuthentication.SignOut();
Session.clear();
Session.Abandon();
HttpCookie cookie = new HttpCookie(UserLoginId);
cookie.value = loginId;
Response.Cookies.Add(cookie);
return RedirectToActionPermanent(Index);
}

在Index.cshtml页面中,我的javascript代码是这样的:

 < script type =text / javascript> 
var jcookie ='@ Request.Cookies [UserLoginId]。Value';
$(#LogInId)。val()= jcookie;
< / script>

但是我没有得到loginId存储在cookie,请帮我这个..


解决方案

如果您需要使用jQuery设置值,请使用 .val(value) jcookie; 。因此,请将您的代码更改为

 < script type =text / javascript 
var jcookie ='@ HttpContext.Current.Request.Cookies [UserLoginId]。Value';
$(#LogInId)。val(jcookie);
< / script>

此外,使用 @ HttpContext.Current.Request @Request



编辑:

 可以请告诉我现在如何设置此UserLoginId值的值为=(我的意思是空白)后,我分配值到$(#LogInId)。 (jcookie);? 

如果您要设置 UserLoginId 为空,为什么要将其保存在cookie中。您可以使用 TempData (例如



在控制器中

  public ActionResult Logout()
{
string loginId = SessionManager.UserloginId;
FormsAuthentication.SignOut();
Session.clear();
Session.Abandon();

//此处使用
TempData [UserLoginId] = loginId;

return RedirectToActionPermanent(Index);
}

在脚本中

 < script type =text / javascript> 
var jcookie ='@TempData [UserLoginId]';
$(#LogInId)。val(jcookie);
< / script>

TempData / p>

I have created the cookie having user login id when user logs out and want to get that user login id in login page to populate the login id text box.

But I am not getting the cookie value in login page javascript function.

My logout action in homecontroller is like this:

public ActionResult Logout()
{  
    string loginId = SessionManager.UserloginId;      
    FormsAuthentication.SignOut();  
    Session.clear();  
    Session.Abandon();  
    HttpCookie cookie = new HttpCookie("UserLoginId");  
    cookie.value = loginId;  
    Response.Cookies.Add(cookie);
    return RedirectToActionPermanent("Index");  
}

And in Index.cshtml page my javascript code is like this:

<script type="text/javascript">
 var jcookie = '@Request.Cookies["UserLoginId"].Value';   
$("#LogInId").val() = jcookie;
</script>

But I am not getting the loginId stored in the cookie, please help me with this..

解决方案

If you need to set value using jQuery use .val(value) instead of $("#LogInId").val() = jcookie;. So change your code as

<script type="text/javascript">
    var jcookie = '@HttpContext.Current.Request.Cookies["UserLoginId"].Value';   
    $("#LogInId").val(jcookie);
</script>

Also, use @HttpContext.Current.Request instead of @Request

EDIT: As per comment

can u please tell me now how to set the value of this UserLoginId value to = ""(I mean blank) after i assign value to $("#LogInId").val(jcookie);?

If you want to set UserLoginId cookie value to blank, why are you saving it in cookie. You can easliy do this using TempData like

In controller

public ActionResult Logout()
{  
    string loginId = SessionManager.UserloginId;      
    FormsAuthentication.SignOut();  
    Session.clear();  
    Session.Abandon();  

    //Used here
    TempData["UserLoginId"] = loginId;  

    return RedirectToActionPermanent("Index");  
}

In Script

<script type="text/javascript">
    var jcookie = '@TempData["UserLoginId"]';   
    $("#LogInId").val(jcookie);
</script>

TempData value will be cleared after use

这篇关于如何获取在控制器中创建的cshtml页面中的javascript中的cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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