CRUD APPLICATION IN ASP.NET MVC (PROJECT MODEL) [PART 5]

Controller Code:

 [HttpGet]
        public ActionResult Login()
        {
            return View();
        }

        [HttpPost]

        public ActionResult Login(tbl_employee emp)
        {
            tbl_employee e = db.tbl_employee.Where(x => x.emp_name == emp.emp_name && x.emp_password == emp.emp_password).SingleOrDefault();

            if (e!=null)
            {
                Session["emp_name"] = e.emp_name;
                Session["emp_id"] = e.emp_id;

                return RedirectToAction("Index");

            }
            else
            {
                ViewBag.error = "Invalid Username or Password";
            }
            return View();
        }


Login View Code:

@model crud_with_miultitables.Models.tbl_employee



@{
    ViewBag.Title = "Register";
}

<h2>LOGIN </h2>

@using (Html.BeginForm("Login", "Employee", FormMethod.Post))
{


    @Html.TextBoxFor(x => x.emp_name, new { @class = "form-control", @placeholder = "Enter Your Name" })

    @Html.PasswordFor(x => x.emp_password, new { @class = "form-control", @placeholder = "Enter Your Password" })


    <input type="submit" value="Login" class="btn btn-success" />

    <div class="form-group">
        <p style="color:red">@ViewBag.error</p>

    </div>

}


Authentication Code:

if (Session["emp_id"] == null)
            {

                return RedirectToAction("Login");
            }

Comments