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 asAlso, use
@HttpContext.Current.Request
instead of@Request
EDIT: As per comment
If you want to set
UserLoginId
cookie value to blank, why are you saving it in cookie. You can easliy do this usingTempData
likeIn controller
In Script
TempData
value will be cleared after usetry this:-