December 17, 2016

Adding a Control to MVC Project

Continuation For :- Simple Login in MVC

Go to Solution Explorer, Right click on Controller folder, Add and then click on Controller.
( Or ) Simply use shortcut key Ctrl + M, Ctrl + C,

  
Provide the Controller Name, and Scaffolding template as LoginController. Then click on Add.


Write the following code in LoginController.


public class LoginController : Controller
    {
        //
        // GET: /Login/
        public ActionResult Login()
        {
            return View();
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Login(M_USER objUser)
        {
            if (ModelState.IsValid)
            {
                using (SampleDataBaseEntities db = new SampleDataBaseEntities())
                {
                    var obj = db.M_USER.Where(a => a.USER_ID.Equals(objUser.USER_ID) && a.PASSWORD.Equals(objUser.PASSWORD)).FirstOrDefault();
                    if (obj != null)
                    {
                        Session["UserID"] = obj.USER_ID.ToString();
                        Session["UserName"] = obj.USER_NAME.ToString();
                        return RedirectToAction("UserDashBoard");
                    }
                }
            }
            return View(objUser);
        }

        public ActionResult UserDashBoard()
        {
            if (Session["UserID"] != null)
            {
                return View();
            }
            else
            {
                return RedirectToAction("Login");
            }
        }
 }

No comments:

Post a Comment