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,passward,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.

Step1: Open Visual Studio 2010.

  • Go to file -> New->Projects.
  • Create an ASP.NET MVC 2 Empty Web Application.
  • Name is "loginform"
starrrrr.gif

Step2: 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

Code:

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

Step3: 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

Code:

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
    {
        //
        // GET: /login/
        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");
        }
    }
}

Step4: Add the Three Views

  • Name of first view is "Index".
createviewcomman.gif

indexview.gif

viewdesign.gif

Code:

<%@ 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>
            Email
        </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>
 

Step5: Add the second view

  • Name of view is "Login view"
createviewcomman.gif

loooooooooooooooooo.gif

logindesign.gif

Code:

<%@ 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>
            <label for="Name">Name:</label>
            <%= Html.TextBox("Nmae") %>
            <%= Html.ValidationMessage("Id", "*") %>
       </p>
      <p>
            <label for="Passward">Passward:</label>
           <%=Html.Password("*") %>
            <%= Html.ValidationMessage("Passward", "*") %>
       </p>
       <p>
            <label for="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>

Step6: Add the third view

  • Name of the view is "recordview"
createviewcomman.gif

recordview.gif

detailview.gif

Code:

<%@ 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>

Step7: Press crtl+f5 run the application.

Output:

outputindex.gif

loginoutput.gif

demmmmmmmmmmmmmmmmmm.gif

Up Next
    Ebook Download
    View all
    Learn
    View all