Thursday, May 31, 2018

Show cookie data one time in MVC



@{
    Response.Cookies["userName"].Value = "Himanshu Gautam";
    Response.Cookies["userName"].Expires = DateTime.Now.AddDays(1);
    Response.Cookies["val"].Value = "0";
    HttpCookie aCookie = new HttpCookie("lastVisit");
    aCookie.Value = DateTime.Now.ToString();
    aCookie.Expires = DateTime.Now.AddDays(1);
    Response.Cookies.Add(aCookie);

    ViewBag.CookieUsername = Server.HtmlEncode(Request.Cookies["userName"].Value);
    ViewBag.CookieLastvisit = Server.HtmlEncode(Request.Cookies["lastVisit"].Value);
    var x = Convert.ToInt16(Server.HtmlEncode(Request.Cookies["val"].Value));
}



@if (x == 0)
{
    <div id="cookiedata">
        Username: @ViewBag.CookieUsername  <br />
        Last Visit: @ViewBag.CookieLastvisit<span><a class="close" onclick="hidediv();">close</a></span>

    </div>

}

<script>
    function hidediv() {
        $('#cookiedata').hide();
        document.cookie="val=1;";
    }
</script>

No comments:

Post a Comment