ARTICLE

Create a Registration Form in ASP.NET MVC

Posted by Manish Singh Articles | ASP.NET MVC with C# December 14, 2011
In this program we will learn to create a registration form used ASP.NET MVC tools.
Reader Level:

Introduction: In this program we will learn to create a registration form used ASP.NET MVC tools. MVC is an integrated module and this is built on three separated words Models,Views,Controllers. Models provide the business logic views provide the presentation and controllers handle the request. Now in this ASP.NET MVC application we will create a registration form first we add the class in the models folder. Second we add a controller in the controller folder and last we create the three views named ndex, create and details. The ASP.NET MVC framework is a lightweight highly testable presentation framework that is integrated with existing ASP.NET features such as master pages and membership based authentication. The MVC framework is defined in the System.Web.Mvc namespace and is a fundamental supported part of the System.Web namespace.

 Step1:  Open Visual Studio 2010.

  • Go to file -> New->Projects.
  • Create an ASP.NET MVC 2 Empty Web Application.
  • Name of "manishRegistration".
str.gif

 start.gif

Step2:  Add a class in the model folder.

  • Right click on the Models folder ->add new items->add class.
  • Name of Class is "manish".
  • In a class define the properties.

addcalaa.gif

classname.gif

Step3: Define the business logic in a class.

Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace MnaishRegistration.Models
{
    public class Mnaish
    {
        public string Email_Id { get; set; }
        public string passward { get; set; }
        public string  BirthYear { get; set; }
        public string IndianZipCode { get; set; }
        public string Gender { get; set; }
    }
}

Step4: Add a controller.

  • Right click on the Controllers folder ->add->Controllers.
  • Name of Controllers is "manu".
  • In a controller, define the request.

addcontroller.gif

controllername.gif

Step5:  Write the code in controller.

Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MnaishRegistration.Models;
namespace MnaishRegistration.Controllers
{
    public class manuController : Controller
    {       
     static List<Mnaish> mm = new List<Mnaish>();
        public ActionResult Index()
        {
            return View(mm);
        }
        public ActionResult Details(Mnaish mm)
        {
            return View(mm);
        }
        public ActionResult Create()
        {
            return View();
        }
         [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create(Mnaish monu)
        {
            if (!ModelState.IsValid)
            {
                return View("Create",monu);
                 }
            mm.Add(monu);
            return RedirectToAction("Index");
        }
    }
}

Step6:  Add the 3 view.

  • The first view is "index".

addview.gif

innnnnn.gif

indexview.gif

Step7:  Write the code in index view.

Code:
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<IEnumerable<MnaishRegistration.Models.Mnaish>>" %>
<!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="#ff9999">
<h2 style="background-color: #FF8080">manish application</h2>
    <div style="background-color: #CC3399">
    <table>
    <tr>
        <th></th>
        <th>
           Email
        </th>
       <th>
|
            Passward
        </th>
        <th>
            BitrhYear
        </th>
        <th>
            INdZipCode
        </th>
        <th>
           Gender
        </th>
    </tr>
<% foreach(var Man in Model) { %>
    <tr>
        <td>  
          <%= Html.ActionLink("Details", "Details", Man)%>

        </td>
        <td>
            <%= Html.Encode(Man.Email_Id) %>
        </td>
        <td>
            <%= Html.Encode(Man.passward) %>
        </td>
        <td>
            <%= Html.Encode(Man.BirthYear) %>
        </td>
        <td>
            <%= Html.Encode(Man.IndianZipCode) %>
        </td>
        <td>
            <%= Html.Encode(Man.Gender) %>
        </td>
    </tr>
<% } %>
</table>
<
p>
    <%= Html.ActionLink("Login Now", "Create") %>
</p>
</
div>
</
body>
</
html>

Step8: Add the second view.

  • the second view is" create".

addview.gif

create.gif

creteview.gif

Step9:  Write the code in create view.

Code:
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<MnaishRegistration.Models.Mnaish>" %>
<!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>Create</title>
</head>
<
body bgcolor="#ffccff">
       <%= Html.ValidationSummary("Create was unsuccessful. Please correct the errors and try again.") %>
<% using (Html.BeginForm()) {%>
    <fieldset>
        <legend>Fields</legend>
        <p>
            <label for="Email_Id">Email_Id:</label>
            <%= Html.TextBox("Email_Id")%>
            <%= Html.ValidationMessage("Email_Id", "*")%>
       </p>
      <p>
            <label for="Passward">Passward:</label>
           <%=Html.Password("*") %>
            <%= Html.ValidationMessage("Passward", "*") %>
       </p>
       <p>
            <label for="BirthYear">BirthYear:</label>
            <%= Html.TextBox("BirthYear")%>
            <%= Html.ValidationMessage("BirthYear", "*")%>
    </p>
     <p>       
    <label for="IndianZipCode ">IndianZipCode :</label>
            <%= Html.TextBox("IndianZipCode ")%>
            <%= Html.ValidationMessage("IndianZipCode ", "*")%>
    </p>
    <p>
            <label for="Gender ">Gender:</label>
            <%= Html.TextBox("Gender ")%>
            <%= Html.ValidationMessage("Gender ", "*")%>
    </p>
    <p>
            <input type="submit" value="Login" />
        </p>
    </fieldset>
<% } %>
    <div>
        <%: Html.ActionLink("Back to List", "Index") %>
    </div>
</body>
</
html>

Step10: Add the third view.

  • the third view is" detail"

addview.gif

detail.gif

detailsview.gif

Step11:  Write the code in Details view.

Code:
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<MnaishRegistration.Models.Mnaish>" %>
<!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>Details</title>
</head>
<
body bgcolor="#ffff99">
    <fieldset>
        <legend>Fields</legend>
        <div class="display-label">Email_Id</div>
        <
div class="display-field"><%: Model.Email_Id %></div>       
        <div class="display-label">passward</div>
        <div class="display-field"><%: Model.passward %></div>
        <div class="display-label">BirthYear</div>
        <div class="display-field"><%: Model.BirthYear %></div>
        <div class="display-label">IndianZipCode</div>
        <div class="display-field"><%: Model.IndianZipCode %></div>
        <div class="display-label">Gender</div>
        <div class="display-field"><%: Model.Gender %></div>
    </fieldset>
    <p>
        <%: Html.ActionLink("Edit", "Edit", new { /* id=Model.PrimaryKey */ }) %> |
        <%: Html.ActionLink("Back to List", "Index") %>
    </p>
</body>
</
html>

Step 12: press crtl+f5 run the application.

Output:

injout.gif

 creteout.gif

 detaiout.gif

 

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

Hi Manish!This is nice article written by you. This is very helpful us. During Searching this topic , i have found two more important article link, that also described in a good way with proper example.-------------------- http://www.mindstick.com/Articles/f769698f-fed6-43eb-8e61-d7baaf713819/?Login%20and%20Registration%20Form ---------------------http://www.c-sharpcorner.com/UploadFile/cd3310/create-registration-form-using-Asp-Net-mvc-tools/

Posted by Ben Reitman Apr 17, 2013

there is error for foreach statment GetEnumaration

Posted by nilesh monde Mar 08, 2013

i want to add ceptcha in mvc2 on login form ..

Posted by Yogendra Gupta Oct 11, 2012

nice..

Posted by Yogendra Gupta Oct 11, 2012

Hi sir,I have created the application as you said only but im getting error on this line<%foreach(var Man in (System.Collections.Generic.IEnumerable <Man> Model){ %>i dont know why...

Posted by KarthicKM M Oct 09, 2012
COMMENT USING
PREMIUM SPONSORS
DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and add new content to existing PDF documents from within your applications.
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.