Create a Register and Login Page in MVC Using LINQ to SQL

This article shows how to create a register and login page in MVC using LINQ to SQL with Model Validation.

Introduction

In this article we will create a register and login page in MVC using LINQ to SQL with Model Validation.

Database structure

Create a table in the database with the name register.

The following is the create table code for the register table:

  1. CREATE TABLE [dbo].[register]
  2.    (  
  3.      [id] [int] IDENTITY(1,1) NOT NULL,  
  4.      [name] [nvarchar](50) NULL,  
  5.      [emailid] [nvarchar](50) NULL,  
  6.      [userpassword] [nvarchar](50) NULL,  
  7.    )  
Create MVC Application

Go to File => New => Project.

Step 2

Choose "ASP.NET MVC 4 Web Application" from the list, then provide the application name as "loginuser" and set the path in the location input where you want to create the application.

Step 3

Now choose the Project Template "Empty".

Adding a LINQ to SQL Class

Step 1

Right-click on the project and select "Add new item". Then, select Data from the templates.

Step 2

Choose "LINQ to SQL classes" from the list and provide a name. Now, after clicking on Add, you can see the .dbml file in the project.

Step 3

Drag the department and register table from the database in the Server Explorer.



Create Model Class

The MVC model contains all the application logic validation, business logic and data access logic. We can create a login class under the Model Folder.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.ComponentModel.DataAnnotations;  
  6. using System.Data.SqlClient;  
  7.   
  8. namespace loginuser.Models  
  9. {  
  10.     public class login  
  11.     {  
  12.         public int id { getset; }  
  13.         [Required]  
  14.         [Display(Name="Name")]  
  15.         public string  Name { getset; }  
  16.         [Required]  
  17.         [Display(Name="Email id")]  
  18.         [RegularExpression(@"\A(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)\Z",  
  19.         ErrorMessage = "Please enter correct email address")]  
  20.         public string  Emailid { getset; }  
  21.         [Display(Name = "Password")]  
  22.   
  23.         [DataType(DataType.Password)]  
  24.   
  25.         [Required(ErrorMessage = "Password required")]  
  26.         public string Userpassword { setget; }  
  27.         [Display(Name = "Confirm new password")]  
  28.         [Required(ErrorMessage = "Enter Confirm Password")]  
  29.         [Compare("Userpassword", ErrorMessage = "The password and confirmation password do not match.")]  
  30.         [DataType(DataType.Password)]  
  31.         public string c_pwd { getset; }  
  32.   
  33.     }  
  34.     public class Enterintotable  
  35.     {  
  36.         public void InsertUser(login li)  
  37.         {  
  38.         DataClasses1DataContext db = new DataClasses1DataContext();  
  39.         register rs = new register();  
  40.         rs.id = li.id;  
  41.         rs.name = li.Name;  
  42.         rs.emailid = li.Emailid;  
  43.         rs.userpassword = li.Userpassword;  
  44.         db.registers.InsertOnSubmit(rs);  
  45.         db.SubmitChanges();  
  46.   
  47.         }  
  48.     }  
  49.     public class Searchuser  
  50.     {  
  51.   
  52.         public  string  searchk(login li)  
  53.         {  
  54.   
  55.             DataClasses1DataContext db = new DataClasses1DataContext();  
  56.             register rs = new register();  
  57.             string passout = "";  
  58.            // var pass = from m in db.registers where m.emailid == li.Emailid select m.userpassword;  
  59.             var pass = from m in db.registers where m.emailid == li.Emailid select m.userpassword;  
  60.             foreach (string query in pass)  
  61.             {  
  62.                 passout = query;  
  63.               
  64.             }  
  65.             return passout;  
  66.           
  67.         }  
  68.         
  69.     }  
  70. }  
Create Loginuser controller
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6. using loginuser.Controllers;  
  7.   
  8. namespace loginuser.Controllers  
  9. {  
  10.     public class LoginuserController : Controller  
  11.     {  
  12.         //  
  13.         // GET: /Loginuser/  
  14.   
  15.         public ActionResult Index()  
  16.         {  
  17.             return View();  
  18.         }  
  19.   
  20.         public ActionResult Signup(loginuser.Models.login li)  
  21.         {  
  22.   
  23.   
  24.             return View(li);  
  25.         }  
  26.   
  27.         public ActionResult SubmitData(loginuser.Models.login li)  
  28.         {  
  29.             if (ModelState.IsValid)  
  30.             {  
  31.                 loginuser.Models.Enterintotable en = new Models.Enterintotable();  
  32.                 en.InsertUser(li);  
  33.                 ViewBag.name = li.Name;  
  34.   
  35.                 // return View();  
  36.                 return View("SubmitData");  
  37.             }  
  38.             else  
  39.             {  
  40.                 return View("Signup");  
  41.             }  
  42.           
  43.         }  
  44.         public ActionResult login(loginuser.Models.login li)  
  45.         {  
  46.             return View(li);  
  47.   
  48.         }  
  49.         public ActionResult Loginsearch(loginuser.Models.login li)  
  50.         {  
  51.             loginuser.Models.Searchuser ss = new Models.Searchuser();  
  52.             string pass=  ss.searchk(li);  
  53.   
  54.            if (pass == li.Userpassword)  
  55.            {  
  56.   
  57.                return View("loadlogin");  
  58.              
  59.            }  
  60.            @ViewBag.data = "invalide user";  
  61.             return View("login" );  
  62.           
  63.         }  
  64.   
  65.         public ActionResult loadlogin()  
  66.         {  
  67.   
  68.             return View();        
  69.         }  
  70.     }  
  71. }  

The Controller has four ActionMethods. In the signup method on the create of a simple register view. If we will click on the create button the value of the textboxes are stored in the database using the submitdata method.

Create a view for the user registration as in the following:

  1. @model loginuser.Models.login  
  2.   
  3. @{  
  4.     Layout = null;  
  5. }  
  6.   
  7. <!DOCTYPE html>  
  8.   
  9. <html>  
  10. <head>  
  11.     <meta name="viewport" content="width=device-width" />  
  12.     <title>Signup</title>  
  13. </head>  
  14. <body>  
  15.         @using (Html.BeginForm("SubmitData", "Loginuser"))  
  16.         {  
  17.             @Html.ValidationSummary(true)  
  18.             {  
  19.   
  20.                 <fieldset>  
  21.                     <legend> Register</legend>  
  22.                     <div class="editor">  
  23.                         @Html.LabelFor(model => model.Name)  
  24.                         @Html.TextBoxFor(model => model.Name)  
  25.                         @Html.ValidationMessageFor(model => model.Name)  
  26.   
  27.                     </div>  
  28.   
  29.                     <div class="editor">  
  30.                         @Html.LabelFor(model => model.Emailid)  
  31.                         @Html.TextBoxFor(model => model.Emailid)  
  32.                         @Html.ValidationMessageFor(model => model.Emailid)  
  33.   
  34.                     </div>  
  35.                     <div class="editor">  
  36.                         @Html.LabelFor(model => model.Userpassword)  
  37.                         
  38.                         @Html.PasswordFor(Model=>Model.Userpassword)  
  39.                         @Html.ValidationMessageFor(model => model.Userpassword)  
  40.   
  41.                     </div>  
  42.                     <div class="editor">  
  43.                         @Html.LabelFor(model => model.c_pwd)  
  44.                          
  45.                         @Html.PasswordFor(Model=>Model.c_pwd)  
  46.                         @Html.ValidationMessageFor(model => model.c_pwd)  
  47.   
  48.                     </div>  
  49.                     <div class="editor">  
  50.                         <input type="submit" value="create" />  
  51.                     </div>  
  52.   
  53.                     @Html.ActionLink("login", "login");  
  54.   
  55.                 </fieldset>  
  56.         }  
  57.     }  
  58.   
  59. </body>  
  60. </html>  

The following is the output of the preceding code.



If we click on the create button. the page redirects to another view name, submitdata.



Create a view for the user login as in the following:

  1. @model loginuser.Models.login  
  2.   
  3. @{  
  4.     ViewBag.Title = "login";  
  5.     //Layout = "~/Views/_ViewStart.cshtml";  
  6. }  
  7.   
  8. <h2>login</h2>  
  9. @using (Html.BeginForm("Loginsearch", "Loginuser"))  
  10. {   
  11.     @Html.ValidationSummary(true)  
  12.     {   
  13.     <fieldset>  
  14.         <legend>Login</legend>  
  15.   
  16.         <div class="editor">  
  17.             @Html.LabelFor(model => model.Emailid)  
  18.             <br />  
  19.             @Html.TextBoxFor(model => model.Emailid)  
  20.             @Html.ValidationMessageFor(model => model.Emailid)  
  21.   
  22.         </div>  
  23.   
  24.         <div class="editor">  
  25.             @Html.LabelFor(model => model.Userpassword)  
  26.             <br />  
  27.             @Html.PasswordFor(Model=>Model.Userpassword)  
  28.              
  29.             @Html.ValidationMessageFor(model => model.Userpassword)  
  30.   
  31.         </div>  
  32.   
  33.         <input type="submit" value="login" />  
  34.         @ViewBag.data  
  35.     </fieldset>      
  36.     }  
  37.   
  38. }  

 

The following is the output of the preceding code.



If we click on the login button the page redirects to another view named home page.


Summary

In this article we learned how to create a register and login page in MVC using LINQ to SQL with model validation.