Building A Simple Registration Form In ASP.NET MVC

Introduction

In this article, I will explain how to build a simple user registration form that will allow users to register using ASP.NET MVC.

Let's understand the MVC architecture in ASP.NET

MVC stands for Model, View and Controller. MVC separates application into three components - Model, View and Controller.

  • Model
    Model represents the shape of the data and business logic. It maintains the data of the application. Model objects retrieve and store model state in a database.

  • View
    View is a user interface. View displays data using model to the user and also enables them to modify the data.

  • Controller
    Controller handles the user request.

The flow of the user's request in ASP.NET MVC.

ASP.Net MVC.

  1. First user enters a URL in the browser
  2. It goes to the server and calls appropriate controller.
  3. The Controller uses the appropriate View and Model and creates the response and sends it back to the user.
  4. Controller handles user's requests, so first we have to create Controller

For creating a registration form we have used one controller, two views and one model class.

Step 1 First Create Table in Database (SQL Server 2012)

  1. Create Database and name it as RegMVC.
  2. Add table (here Table name: tblRegistration)
  3. Set primary key to Id column.

    ASP.Net MVC.

Note

In this Example I set the Id to auto increment so that the id will be automatically generated for every new added row. To do this select the Column name Id and in the column properties set the Identity Specification to yes.

ASP.Net MVC.

Step 2 Create new project in Visual Studio 2015

  1. Open Visual studio 2015-> click on file -> new-> project.

    ASP.Net MVC.

  2. Visual C#-> Web -> ASP.NET Web Application-> Write Project Name-> OK.

    ASP.Net MVC.

  3. Select MVC template.

    Empty-> Click MVC Folder-> OK.

    ASP.Net MVC.

  4. MVC Folders are created.

    ASP.Net MVC.

Step 3 ADD ENTITY DATA MODEL

  1. Right click Models Folder-> Add-> Class-> Visual C#-> Data-> ADO.NET Entity data Model-> Entry Name-> ADD

    ASP.Net MVC.

    ASP.Net MVC.

  2. Entity Data Model Wizard dialog.

    Select EF Designer from Database-> Next->New Connection

    ASP.Net MVC.

    ASP.Net MVC.

  3. Enter your server name-> Choose your authentication, I am using SQL Server authentication. So we need to provide User Name and Password-> Select your database-> Test Connection-> ok.

    ASP.Net MVC.

  4. Includes the sensitive data in the connection string->next->Choose version of Entity Framework.

    ASP.Net MVC.

  5. Include Database objects to your model-> Finish.

    ASP.Net MVC.

  6. Visual Studio will generate a database diagram for the table.

    ASP.Net MVC.

    Now, you have an Entity Data Model file in your Model folder with all its necessary supportive files.

    The auto generated entities file with DbContext inherited (lRegistrationt.Context.cs).DbContext and DbSet that we need to establish a link between the model and the database."tblRegistration.cs" class, you will see all the properties.(Model Class).

  7. Model class is ready with all Properties(tblRegistration.cs file)

    ASP.Net MVC.

Step 4  CREATE A CONTROLLER

  1. Go to the controllers folder-> Add-> Controller-> Select MVC 5 Controller Empty->Add.

    ASP.Net MVC.

  2. It will open Add Controller dialog as shown below.

    ASP.Net MVC.

  3. Write Controller Name. Do not delete the word Controller. Remember, controller name must end with Controller.

    ASP.Net MVC.

  4. This will create HomeController class with Index method in HomeController.cs file under Controllers folder, as shown below.

    ASP.Net MVC.

Step 5 Add View

  1. Open a HomeController class -> right click inside Index method -> click Add View.

    ASP.Net MVC.

  2. In the Add View dialogue box, keep the view name as Index. Choose Template Create (because we are inserting data in this Application). Select Model Class. Select Data Context Class. Click Add Button.

    ASP.Net MVC.

    NOTE

    You will find this error. Just Rebuild the Project.

    ASP.Net MVC.

  3. After creating the view, it will look like below.
    1. @model Registration.Models.tblRegistration  
    2. @ {  
    3.     Layout = null;  
    4. } <  
    5. !DOCTYPE html >  
    6.     <  
    7.     html >  
    8.     <  
    9.     head >  
    10.     <  
    11.     meta name = "viewport"  
    12. content = "width=device-width" / >  
    13.     <  
    14.     title > Index < /title> <  
    15.     /head> <  
    16.     body >  
    17.     @using(Html.BeginForm()) {  
    18.         @Html.AntiForgeryToken()  
    19.   
    20.         <  
    21.         div class = "form-horizontal" >  
    22.             <  
    23.             h4 > tblRegistration < /h4> <  
    24.             hr / >  
    25.             @Html.ValidationSummary(true""new {  
    26.                 @class = "text-danger"  
    27.             }) <  
    28.             div class = "form-group" >  
    29.             @Html.LabelFor(model => model.FName, htmlAttributes: new {  
    30.                 @class = "control-label col-md-2"  
    31.             }) <  
    32.             div class = "col-md-10" >  
    33.             @Html.EditorFor(model => model.FName, new {  
    34.                 htmlAttributes = new {  
    35.                     @class = "form-control"  
    36.                 }  
    37.             })  
    38.         @Html.ValidationMessageFor(model => model.FName, ""new {  
    39.                 @class = "text-danger"  
    40.             }) <  
    41.             /div> <  
    42.             /div> <  
    43.             div class = "form-group" >  
    44.             @Html.LabelFor(model => model.LName, htmlAttributes: new {  
    45.                 @class = "control-label col-md-2"  
    46.             }) <  
    47.             div class = "col-md-10" >  
    48.             @Html.EditorFor(model => model.LName, new {  
    49.                 htmlAttributes = new {  
    50.                     @class = "form-control"  
    51.                 }  
    52.             })  
    53.         @Html.ValidationMessageFor(model => model.LName, ""new {  
    54.                 @class = "text-danger"  
    55.             }) <  
    56.             /div> <  
    57.             /div> <  
    58.             div class = "form-group" >  
    59.             @Html.LabelFor(model => model.Password, htmlAttributes: new {  
    60.                 @class = "control-label col-md-2"  
    61.             }) <  
    62.             div class = "col-md-10" >  
    63.             @Html.EditorFor(model => model.Password, new {  
    64.                 htmlAttributes = new {  
    65.                     @class = "form-control"  
    66.                 }  
    67.             })  
    68.         @Html.ValidationMessageFor(model => model.Password, ""new {  
    69.                 @class = "text-danger"  
    70.             }) <  
    71.             /div> <  
    72.             /div> <  
    73.             div class = "form-group" >  
    74.             @Html.LabelFor(model => model.City, htmlAttributes: new {  
    75.                 @class = "control-label col-md-2"  
    76.             }) <  
    77.             div class = "col-md-10" >  
    78.             @Html.EditorFor(model => model.City, new {  
    79.                 htmlAttributes = new {  
    80.                     @class = "form-control"  
    81.                 }  
    82.             })  
    83.         @Html.ValidationMessageFor(model => model.City, ""new {  
    84.                 @class = "text-danger"  
    85.             }) <  
    86.             /div> <  
    87.             /div>  
    88.   
    89.         <  
    90.         div class = "form-group" >  
    91.             <  
    92.             div class = "col-md-offset-2 col-md-10" >  
    93.             <  
    94.             input type = "submit"  
    95.         value = "Create"  
    96.         class = "btn btn-default" / >  
    97.             <  
    98.             /div> <  
    99.             /div> <  
    100.             /div>  
    101.     }  
    102.   
    103. <  
    104. /body> <  
    105. /html>  

Step 6 ADD NEW ACTION INTO YOUR CONTROLLER FOR GET METHOD

  1. Add following Namespaces in Controller.
    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Linq;  
    4. using System.Web;  
    5. using System.Web.Mvc;  
    6. using Registration.Models;  
    Write following code in Controller Class.( HomeController.CS Class)
    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Linq;  
    4. using System.Web;  
    5. using System.Web.Mvc;  
    6. using Registration.Models;  
    7.   
    8. namespace Registration.Controllers {  
    9.     public class HomeController: Controller {  
    10.   
    11.         public ActionResult Index() {  
    12.             return View();  
    13.         }  
    14.   
    15.         [HttpPost]  
    16.         public ActionResult Index(tblRegistration obj)  
    17.   
    18.         {  
    19.             if (ModelState.IsValid) {  
    20.                 RegMVCEntities db = new RegMVCEntities();  
    21.                 db.tblRegistrations.Add(obj);  
    22.                 db.SaveChanges();  
    23.             }  
    24.             return View(obj);  
    25.         }  
    26.     }  
    27. }  

Code Explanation

  1. Home Controller Class Contain two Action methods. First Action method is [HttpGet], when you run the application this method will execute. Second Action method is [HttpPost], when user clicks Create button this Action method will execute.
  2. Modelstate.IsValid property validates each model property against the validation attributes used in the Model and it returns true or false.
  3. SaveChanges() method is called to save the data into the database.

Output

ASP.Net MVC.

In Database

ASP.Net MVC.

Up Next
    Ebook Download
    View all
    Learn
    View all