Create a simple login form in MVC ASP.NET

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"
starrrrr.gif

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.
strtmodeladdcalss.gif

classadd.gif

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. namespace Loginform.Models  
  6. {  
  7.     public class sonal  
  8.     {  
  9.         public string Name { getset; }  
  10.         public int Passward { getset; }  
  11.         public string Email { getset; }  
  12.     }  
  13. }  
Step 3: Add a controller.
  • Right click on the Controllers folder ->add->Controllers.
  • Name of Controllers is "Login".
  • In a controller, define the request.
addcontroller.gif

controller.gif

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6. using Loginform.Models;  
  7. namespace Loginform.Controllers  
  8. {  
  9.     public class loginController : Controller  
  10.     {  
  11.         //  
  12.         // GET: /login/  
  13.         static List<sonal> sun = new List<sonal>();  
  14.         public ActionResult Index()  
  15.         {  
  16.             return View(sun);  
  17.         }  
  18.         public ActionResult Record(sonal sun)  
  19.         {  
  20.             return View(sun);  
  21.         }  
  22.         public ActionResult Login()  
  23.         {  
  24.             return View();  
  25.         }  
  26.         [AcceptVerbs(HttpVerbs.Post)]  
  27.         public ActionResult Login(sonal sonu)  
  28.         {  
  29.             if (!ModelState.IsValid)  
  30.             {  
  31.                 return View("Login", sonu);  
  32.             }  
  33.             sun.Add(sonu);  
  34.             return RedirectToAction("Index");  
  35.         }  
  36.     }  
  37. }  
Step 4: Add the Three Views
  • Name of first view is "Index".
createviewcomman.gif

indexview.gif

viewdesign.gif

  1. <%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<IEnumerable<Loginform.Models.sonal>>" %>  
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  3. <html xmlns="http://www.w3.org/1999/xhtml" >  
  4. <head runat="server">  
  5.     <title>Index</title>  
  6. </head>  
  7. <body bgcolor="#66ffcc">  
  8.     <div>  
  9.    <table>  
  10.     <tr>  
  11.         <th></th>  
  12.         <th>  
  13.            Nmae  
  14.         </th>  
  15.         <th>  
  16.             Email  
  17.         </th>  
  18.     </tr>  
  19. <% foreach (var sonal in Model) { %>  
  20.     <tr>  
  21.         <td>  
  22.             <%= Html.ActionLink("Record""Record",sonal )%>  
  23.         </td>  
  24.         <td>  
  25.             <%= Html.Encode(sonal.Name) %>  
  26.         </td>  
  27.         <td>  
  28.             <%= Html.Encode(sonal.Email) %>  
  29.         </td>  
  30.     </tr>  
  31. <% } %>  
  32. </table>  
  33. <p>  
  34.     <%= Html.ActionLink("Login Now""Login") %>  
  35. </p>  
  36.     </div>  
  37. </body>  
  38. </html>  
Step 5: Add the second view
  • Name of view is "Login view"
createviewcomman.gif

loooooooooooooooooo.gif

logindesign.gif
  1. <%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<Loginform.Models.sonal>" %>  
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  3. <html xmlns="http://www.w3.org/1999/xhtml" >  
  4. <head runat="server">  
  5.     <title>Login</title>  
  6. </head>  
  7. <body bgcolor="#ff80c0">  
  8.     <div>  
  9.      <h2 >Login</h2>  
  10. <%= Html.ValidationSummary("Create was unsuccessful. Please correct the errors and try again.") %>  
  11. <% using (Html.BeginForm()) {%>  
  12.     <fieldset>  
  13.         <legend>Fields</legend>  
  14.         <p>  
  15.             <label for="Name">Name:</label>  
  16.             <%= Html.TextBox("Nmae") %>  
  17.             <%= Html.ValidationMessage("Id""*") %>  
  18.        </p>  
  19.       <p>  
  20.             <label for="Passward">Passward:</label>  
  21.            <%=Html.Password("*") %>  
  22.             <%= Html.ValidationMessage("Passward""*") %>  
  23.        </p>  
  24.        <p>  
  25.             <label for="Email">Email:</label>  
  26.             <%= Html.TextBox("Email") %>  
  27.             <%= Html.ValidationMessage("Email""*") %>  
  28.     </p>  
  29.     <p>  
  30.             <input type="submit" value="Login" />  
  31.         </p>  
  32.     </fieldset>  
  33. <% } %>  
  34. <div>  
  35.     <%=Html.ActionLink("Back to List""Index") %>  
  36. </div>  
  37.     </div>  
  38. </body>  
  39. </html>  
Step 6: Add the third view
  • Name of the view is "recordview"
createviewcomman.gif

recordview.gif

detailview.gif

  1. <%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<Loginform.Models.sonal>" %>  
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  3. <html xmlns="http://www.w3.org/1999/xhtml" >  
  4. <head runat="server">  
  5.     <title>Record</title>  
  6. </head>  
  7. <body bgcolor="#ff80ff">  
  8.     <div>  
  9.     <h2>Details</h2>  
  10. <fieldset>  
  11.     <legend>Display Fields</legend>  
  12.     <p>  
  13.         Nmae:  
  14.         <%= Html.Encode(Model.Name) %>  
  15.     </p>  
  16.     <p>  
  17.         Passward:  
  18.         <%= Html.Encode(Model.Passward) %>  
  19.     </p>  
  20.     <p>  
  21.         Email:  
  22.         <%= Html.Encode(Model.Email) %>  
  23.     </p>  
  24. </fieldset>  
  25. <p>  
  26.     <%=Html.ActionLink("Back to List""Index") %>  
  27. </p>  
  28.     </div>  
  29. </body>  
  30. </html>  
Step 7: Press crtl+f5 run the application.

outputindex.gif

loginoutput.gif

demmmmmmmmmmmmmmmmmm.gif


Similar Articles