December 31, 2016

Simple Login Application using Sessions in ASP.NET MVC

Continuation Form : Creating your first MVC Project

Step 1: Add Entity Data Model
Go to Solution Explorer, Right Click on Project, Add, New Item, then select ADO.NET Entity Data Model.


Give it a meaningful model name and then click on Add.


 Select Generate from database and then click on Next.

Click on New Connection,


After clicking on New Connection, we have to provide the required information

Click on the "Test Connection" button to ensure the connection can be established.
Then click OK. 

Select radio button: Yes include the sensitive data in the connection string.

Choose your database objects, as in the following image.


 Click on Finish. At this point UserProfie entity will be created.

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");
            }
        }
 }

December 3, 2016

Creating View in MVC

Create View for Login Action Method
Right click on the Login Action method, then click on Add View as in the following picture.

Give a name for the View
Click Add.