Simple and Easy Mvc Login With Multiple Role
- Updated date Apr 02, 2019
- 790.9k
- 12
In this article, we are creating a simple login form application using ASP.NET MVC.
Introduction
Using ASP.NET MVC tools we have created a simple login form application. In this application we have created three fields: name, password, email. The Microsoft ASP.NET MVC framework is Microsoft's newest framework for building web applications. The ASP.NET MVC framework was designed from the ground up to make it easier to build good software. The ASP.NET MVC framework was created to support pattern-based software development. In other words the framework was designed to make it easier to implement software design principles and patterns when building web applications. MVC model contains all of an application's logic that is not contained in a View or Controller. MVC view contains HTML markup and view logic. MVC controller contains control-flow logic. An MVC controller interacts with MVC Models and Views to control the flow of application execution.
Step 1: Open Visual Studio 2010.
- Go to file -> New->Projects.
- Create an ASP.NET MVC 2 Empty Web Application.
- Name is "loginform"
Step 2: Add a class in model folder.
- Right click on the Model folder ->add new items->add class.
- Name of Class is "sonal".
- In a class define the properties.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- namespace Loginform.Models
- {
- public class sonal
- {
- public string Name { get ; set ; }
- public int Passward { get ; set ; }
- public string Email { get ; set ; }
- }
- }
Step 3: Add a controller.
- Right click on the Controllers folder ->add->Controllers.
- Name of Controllers is "Login".
- In a controller, define the request.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- using Loginform.Models;
- namespace Loginform.Controllers
- {
- public class loginController : Controller
- {
- static List<sonal> sun = new List<sonal>();
- public ActionResult Index()
- {
- return View(sun);
- }
- public ActionResult Record(sonal sun)
- {
- return View(sun);
- }
- public ActionResult Login()
- {
- return View();
- }
- [AcceptVerbs(HttpVerbs.Post)]
- public ActionResult Login(sonal sonu)
- {
- if (!ModelState.IsValid)
- {
- return View( "Login" , sonu);
- }
- sun.Add(sonu);
- return RedirectToAction( "Index" );
- }
- }
- }
Step 4: Add the Three Views
- Name of first view is "Index".
- <%@ Page Language= "C#" Inherits= "System.Web.Mvc.ViewPage<IEnumerable<Loginform.Models.sonal>>" %>
- <!DOCTYPE html PUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
- <html xmlns="http://www.w3.org/1999/xhtml" >
- <head runat="server" >
- <title>Index</title>
- </head>
- <body bgcolor="#66ffcc" >
- <div>
- <table>
- <tr>
- <th></th>
- <th>
- Nmae
- </th>
- <th>
- </th>
- </tr>
- <% foreach (var sonal in Model) { %>
- <tr>
- <td>
- <%= Html.ActionLink("Record" , "Record" ,sonal )%>
- </td>
- <td>
- <%= Html.Encode(sonal.Name) %>
- </td>
- <td>
- <%= Html.Encode(sonal.Email) %>
- </td>
- </tr>
- <% } %>
- </table>
- <p>
- <%= Html.ActionLink("Login Now" , "Login" ) %>
- </p>
- </div>
- </body>
- </html>
Step 5: Add the second view
- Name of view is "Login view"
- <%@ Page Language= "C#" Inherits= "System.Web.Mvc.ViewPage<Loginform.Models.sonal>" %>
- <!DOCTYPE html PUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
- <html xmlns="http://www.w3.org/1999/xhtml" >
- <head runat="server" >
- <title>Login</title>
- </head>
- <body bgcolor="#ff80c0" >
- <div>
- <h2 >Login</h2>
- <%= Html.ValidationSummary("Create was unsuccessful. Please correct the errors and try again." ) %>
- <% using (Html.BeginForm()) {%>
- <fieldset>
- <legend>Fields</legend>
- <p>
- <labelfor = "Name" >Name:</label>
- <%= Html.TextBox("Nmae" ) %>
- <%= Html.ValidationMessage("Id" , "*" ) %>
- </p>
- <p>
- <labelfor = "Passward" >Passward:</label>
- <%=Html.Password("*" ) %>
- <%= Html.ValidationMessage("Passward" , "*" ) %>
- </p>
- <p>
- <labelfor = "Email" >Email:</label>
- <%= Html.TextBox("Email" ) %>
- <%= Html.ValidationMessage("Email" , "*" ) %>
- </p>
- <p>
- <input type="submit" value= "Login" />
- </p>
- </fieldset>
- <% } %>
- <div>
- <%=Html.ActionLink("Back to List" , "Index" ) %>
- </div>
- </div>
- </body>
- </html>
Step 6: Add the third view
- Name of the view is "recordview"
- <%@ Page Language= "C#" Inherits= "System.Web.Mvc.ViewPage<Loginform.Models.sonal>" %>
- <!DOCTYPE html PUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
- <html xmlns="http://www.w3.org/1999/xhtml" >
- <head runat="server" >
- <title>Record</title>
- </head>
- <body bgcolor="#ff80ff" >
- <div>
- <h2>Details</h2>
- <fieldset>
- <legend>Display Fields</legend>
- <p>
- Nmae:
- <%= Html.Encode(Model.Name) %>
- </p>
- <p>
- Passward:
- <%= Html.Encode(Model.Passward) %>
- </p>
- <p>
- Email:
- <%= Html.Encode(Model.Email) %>
- </p>
- </fieldset>
- <p>
- <%=Html.ActionLink("Back to List" , "Index" ) %>
- </p>
- </div>
- </body>
- </html>
Step 7: Press crtl+f5 run the application.
Source: https://www.c-sharpcorner.com/UploadFile/cd3310/using-mvc-Asp-Net-tools-create-simple-login-form/