当用户注销时,我已经创建了具有用户登录id的cookie,并希望在登录页面中获取该用户登录id,以填充登录id文本框。
但是我不会在登录页面javascript函数中获得cookie值。
我在logout中的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储存在饼干里,请帮我拿这个。
发布于 2013-10-22 06:23:11
如果需要使用jQuery设置值,请使用.val(value)而不是$("#LogInId").val() = jcookie;。因此,请将代码更改为
<script type="text/javascript">
var jcookie = '@HttpContext.Current.Request.Cookies["UserLoginId"].Value';
$("#LogInId").val(jcookie);
</script>另外,使用@HttpContext.Current.Request而不是@Request
编辑:根据评论
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);?如果要将UserLoginId cookie值设置为空,为什么要将其保存在cookie中。您可以使用TempData轻松地完成此操作,例如
在控制器中
public ActionResult Logout()
{
string loginId = SessionManager.UserloginId;
FormsAuthentication.SignOut();
Session.clear();
Session.Abandon();
//Used here
TempData["UserLoginId"] = loginId;
return RedirectToActionPermanent("Index");
}在剧本中
<script type="text/javascript">
var jcookie = '@TempData["UserLoginId"]';
$("#LogInId").val(jcookie);
</script>TempData值将在使用后清除。
发布于 2013-10-22 06:17:51
试试这个:-
<Script type="test/javascript">
var jcookie = '@Request.Cookies["UserLoginId"].Value';
$("#loginId").val(jcookie)
</Script>https://stackoverflow.com/questions/19510291
复制相似问题