SIGN UP MEMBER LOGIN:    
ARTICLE

Create a simple login form in MVC ASP.NET

Posted by Manish Singh Articles | ASP.NET MVC with C# December 04, 2011
In this application I create a login form with three fields: name, passward and email. The Microsoft ASP.NET MVC framework is Microsoft's newest framework for building web applications.
Reader Level:

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

Login to add your contents and source code to this article
share this article :
post comment
 

Hey..i m getting an error hile creating dis in my application like "The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[Final_MLM.Login]', but this dictionary requires a model item of type 'Final_MLM.Models.Login'."... plz help to solve dis error..

Posted by Pooja Doshi Mar 30, 2012

This is a good one to understand the MVC

Posted by Neeraj Rai Dec 07, 2011
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Nevron Gauge for SharePoint
Become a Sponsor