Project Overview:
- HomeController.cs
- Index() -> load default view of the HomeController. Where view is located at Views->Home->index.cshtml
- Index(LoginModel model)-> This received the data input by the user from our login form using a POST request.This return default view of the Controller which is the login form if the login details is incorrect or redirect to the landing page if successfully authenticated.
- AccountVerification()-> This function return boolean(true or false). This check the login credential if it is valid or not.
- Welcome_Page()-> This call’s the landing view if the user is successfully login.
- LoginModel()-> This is a class where properties are being declared. (public string Email{get;set}). We will use this model to bind with our login view form.
- chtml-> This is the Default view of Home Controller (Location Views->Home->Index.cshtml.
Welcome_Page.cshtml -> view for Welcome_Page ActionResult this serve as our UserInterface for our landing page.(Location Views->Home->Welcome_Page.cshtml)
Project output:
Let’s Start:
Note: Name your project as Simple MVC Login implementation
First create new project in Visual Studio 2013 using a default MVC Template. If this is your first time to create one visit this blog for easy instruction in creating a project How to start with Asp.Net MVC application.
After successfully creating a project go to solution explorer and navigate to Model folder and add new class for our model. Right click on Model folder choose addthen select Class.
Name your class as LoginModel.
Open your LoginModel class and copy and paste code below:
LoginModel.cs Code:
- Declare Email and password properties and define property requirements.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
namespace Simple_MVC_Login_implementation.Models
{
public class LoginModel
{
[Required]
[Display(Name="Email")]
public string Email { get; set; }
[Required]
[DataType(DataType.Password)]
[Display(Name="Password")]
public string Password { get; set; }
}
}
Note:
- using System.ComponentModel.DataAnnotations; -> This is responsible for setting attribute and requirements for your properties. Ex. [Required]
Next, go to Controller folder and open file HomeController. HomeController is the default controller for generating the default view or User Interface of your web application. This is where we put all the Process we will need for the project.
Replace the code from your HomeController with the code below.
Home Controller Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Simple_MVC_Login_implementation.Models;
namespace Simple_MVC_Login_implementation.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(LoginModel model)
{
if (!ModelState.IsValid)
{
return View(model);
}
if (AccountVerification(model.Email, model.Password))
{
return RedirectToAction("Welcome_Page",model);
}
else
{
ModelState.AddModelError("", "Invalid Input Details");
}
return View();
}
public bool AccountVerification(string Email,string password)
{
bool result = false;
if (Email == "test@gmail.com" && password == "12345")
{
result = true;
}
return result;
}
public ActionResult Welcome_Page(LoginModel model)
{
return View(model);
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Simple_MVC_Login_implementation.Models;
namespace Simple_MVC_Login_implementation.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(LoginModel model)
{
if (!ModelState.IsValid)
{
return View(model);
}
if (AccountVerification(model.Email, model.Password))
{
return RedirectToAction("Welcome_Page",model);
}
else
{
ModelState.AddModelError("", "Invalid Input Details");
}
return View();
}
public bool AccountVerification(string Email,string password)
{
bool result = false;
if (Email == "test@gmail.com" && password == "12345")
{
result = true;
}
return result;
}
public ActionResult Welcome_Page(LoginModel model)
{
return View(model);
}
}
}
HomeController function details:
- Index() -> This return Default view of HomeController which is our Login Form
- Index(LoginModel model) – > This receive model data from our view (Login Form) using a post request. It also redirects the page to our welome_page view if credential is successfully validated.
- AccountVerification(string Email,string password) – > This function use to verify if email and password match to your valid credential.
- ModelState.IsValid -> Controller properties that check model property state.
Index.cshtml Code:
Output:
Open your HomeController default view which is index.cshtml located at your solution explorer Views->Home->Index.cshtml.
To bind our Model to our Login form use html helper to generate your form.
Copy and paste code below to your index.cshtml.
- @using Simple_MVC_Login_implementation.Models -> access model properties. @model LoginModelBind LoginModel to our View.
Code below creates a simple login form design.
@model LoginModel
@model LoginModel
@{
ViewBag.Title = "Home Page";
}
<div class="row">
<br/>
@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { @class = "form-horizontal",role="form" }))
{
<fieldset>
@Html.ValidationSummary(true, "", new { @class = "text-danger" }) <!--Enable Data Anotation-->
<div class="form-group">
@Html.LabelFor(m => m.Email, new { @class = "col-lg-2 control-label"})
<div class="col-lg-10">
@Html.TextBoxFor(m => m.Email, new { @class = "form-control"})
@Html.ValidationMessageFor(m => m.Email, "", new { @class="text-danger"}) <!Display of Error Notification-->
</div>
</div>
<div class="form-group">
@Html.LabelFor(m => m.Password, new { @class = "col-lg-2 control-label"})
<div class="col-lg-10">
@Html.PasswordFor(m => m.Password, new { @class = "form-control" })
@Html.ValidationMessageFor(m => m.Password, "", new { @class = "text-danger" })<!Display of Error Notification-->
</div>
</div>
<div class="form-group">
<div class="col-lg-10 col-lg-offset-2">
<button type="submit" class="btn btn-primary">Login</button>
</div>
</div>
</fieldset>
}
</div>
Lastly, create a landing page or welcome page for your application. This will be displayed after a user has successfully validated his credentials.
To add new View, from your solution explorer right click on the Home folder inside the views folder and choose Add then select MVC 5 View Page with Layout (Razor).
Then choose layout for your view.
Then choose layout for your view.
Open Welcome_Page view. Copy and paste code below:
Welcome_Page Code:
- Display welcome message including the email used to log in.
@using Simple_MVC_Login_implementation.Models
@model LoginModel
@{
Layout = "~/Views/Shared/_Layout.cshtml";
}
<div class="jumbotron">
<h2>Welcome To My Page!</h2>
<h3>You are Log in as @Model.Email</h3>
</div>
Output:
No comments:
Post a Comment