using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MVC_Cookies.Controllers
{
   
public class HomeController : Controller
   
{ 
            public ActionResult Index()
            {
                HttpCookie cookie =
Request.Cookies["UserDetail"];
                if (cookie != null)
                {
                    Tbl_User obj = new Tbl_User();
                    obj.UserName = cookie["Name"];
                    return View(obj);
                }
                return View();
            }
            [HttpPost]
            public ActionResult Index(Tbl_User objUser, string chk)
            {
                if (ModelState.IsValid)
                {
                    using (Db_EFEntities db = new Db_EFEntities())
                    {
                        Tbl_User u =
db.Tbl_User.Where<Tbl_User>(obj => obj.UserName == objUser.UserName
&& obj.Password == objUser.Password).FirstOrDefault<Tbl_User>();
                        if (u != null)
                        {
                            if (chk == "check")
                            {
                                HttpCookie ck =
new HttpCookie("UserDetail");
                                ck["Name"] = objUser.UserName;
                                ck.Expires =
DateTime.Now.AddDays(30);
                               
Response.Cookies.Add(ck);
                            }
                        }
                        else
                        {
   
                        ModelState.AddModelError(string.Empty, "There is no
such user");
                            return View();
                        }
                    }
                }
                return View("linkpage");
            }
            public ActionResult UserDashBoard()
            {
                return View();
            }
            public ActionResult Delete()
            {
                if (Request.Cookies["UserDetail"] != null)
                {
                    HttpCookie myCookie = new HttpCookie("UserDetail");
                    myCookie.Expires =
DateTime.Now.AddDays(-1d);
                   
Response.Cookies.Add(myCookie);
                }
                return RedirectToAction("Index");
            }
       
   
}
}
index.cshtml
@model
MVC_Cookies.Tbl_User
@{
   
ViewBag.Title = "Index";
}
<h2>Index</h2>
@using (Html.BeginForm()) 
{
   
@Html.AntiForgeryToken()
   
   
<div class="form-horizontal">
       
<h4>Tbl_User</h4>
       
<hr />
       
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
       
<div class="form-group">
            @Html.LabelFor(model => model.UserName,
htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.UserName, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model =>
model.UserName, "", new { @class = "text-danger" })
            </div>
       
</div>
 
      <div class="form-group">
            @Html.LabelFor(model => model.Password,
htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Password, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model =>
model.Password, "", new { @class = "text-danger" })
            </div>
       
</div>
       
<div>
            <input type="checkbox" name="chk" value="check" /> 
       
</div>
       
<div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Login" class="btn
btn-default" />
            </div>
       
</div>
   
</div>
}
<div>
   
@Html.ActionLink("Back to
List",
"Index")
</div>
linkpage.cshtml
@model
MVC_Cookies.Tbl_User
@{
   
ViewBag.Title = "linkpage";
}
<h2>linkpage</h2>
@Html.ActionLink("chk
cookies", "UserDashBoard")
@{
   
ViewBag.Title = "UserDashBoard";
}
UserDashBoard.cshtml
@model
MVC_Cookies.Tbl_User
@{
   
ViewBag.Title = "UserDashBoard";
}
<h2>UserDashBoard</h2>
<fieldset>
   
<legend> User DashBoard </legend>
   
@{
       
HttpCookie cookie = Request.Cookies["UserDetail"];
       
if (cookie != null)
       
{
            <text>   
Welcome @cookie["Name"] </text>
       
}
       
else
       
{ <text>
no data in cookie</text>}
   
}
</fieldset>
 
No comments: