Step 1: Create Registration Table.

Create Registration Table

Create table

  1. CREATE TABLE [dbo].[Registration1](
  2. [UserID] [int] IDENTITY(1,1) NOT NULL,
  3. [Username] [varchar](50) NOT NULL,
  4. [Password] [varchar](50) NOT NULL,
  5. [FullName] [varchar](100) NOT NULL,
  6. [EmailID] [varchar](200) NULL,
  7. PRIMARY KEY CLUSTERED
  8. (
  9. [UserID] ASC
  10. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
  11. ) ON [PRIMARY]
Step 2: Create a project.

Go to File, then New and click Project. Select ASP.NET MVC 4 Web Application and enter the project name, then click OK, select Empty, select View Engine Razor and press OK.

Step 3: Add model.

add class

enter class name

user.cs
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel.DataAnnotations;
  4. using System.Linq;
  5. using System.Web;
  6. using System.Web.Mvc;
  7. namespace MVCRegistration.Models {
  8. public class User {
  9. public int UserID {
  10. get;
  11. set;
  12. }
  13. [Required(ErrorMessage = "Please provide username", AllowEmptyStrings = false)]
  14. public string Username {
  15. get;
  16. set;
  17. }
  18. [Required(ErrorMessage = "Please provide Password", AllowEmptyStrings = false)]
  19. [DataType(System.ComponentModel.DataAnnotations.DataType.Password)]
  20. [StringLength(50, MinimumLength = 8, ErrorMessage = "Password must be 8 char long.")]
  21. public string Password {
  22. get;
  23. set;
  24. }
  25. [Compare("Password", ErrorMessage = "Confirm password dose not match.")]
  26. [DataType(System.ComponentModel.DataAnnotations.DataType.Password)]
  27. public string ConfirmPassword {
  28. get;
  29. set;
  30. }
  31. [Required(ErrorMessage = "Please provide full name", AllowEmptyStrings = false)]
  32. public string FullName {
  33. get;
  34. set;
  35. }
  36. [RegularExpression(@
  37. "^([0-9a-zA-Z]([\+\-_\.][0-9a-zA-Z]+)*)+@(([0-9a-zA-Z][-\w]*[0-9a-zA-Z]*\.)+[a-zA-Z0-9]{2,3})$",
  38. ErrorMessage = "Please provide valid email id")]
  39. public string EmailID {
  40. get;
  41. set;
  42. }
  43. }
  44. }
Step 4: Add Data Enity Model

Add Data Enity Model

select connection

table

navigation properties

Step 5: Add UserController.

add controller

code

Usercontroller.cs
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Mvc;
  6. using MVCRegistration.Models;
  7. namespace MVCRegistration.Controllers {
  8. public class UserController: Controller {
  9. //
  10. // GET: /User/
  11. public ActionResult Register() {
  12. return View();
  13. }
  14. [HttpPost]
  15. [ValidateAntiForgeryToken]
  16. public ActionResult Register(Registration1 U) {
  17. if (ModelState.IsValid) {
  18. using(RBACEntities dc = new RBACEntities()) {
  19. dc.Registration1.Add(U);
  20. dc.SaveChanges();
  21. ModelState.Clear();
  22. U = null;
  23. ViewBag.Message = "Successfully Registration Done";
  24. }
  25. }
  26. return View(U);
  27. }
  28. }
  29. }
Step 6: Add View.

add view

Register.cshtml

  1. @model MVCRegistration.Models.User
  2. @{
  3. ViewBag.Title = "Register";
  4. }
  5. <h2>Register</h2>
  6. @using (Html.BeginForm()) {
  7. @Html.ValidationSummary(true)
  8. <fieldset>
  9. <legend>User</legend>
  10. @Html.AntiForgeryToken()
  11. @if (ViewBag.Message != null)
  12. {
  13. <div style="border:solid 1px green">
  14. @ViewBag.Message
  15. </div>
  16. }
  17. <div class="editor-label">
  18. @Html.LabelFor(model => model.FullName)
  19. </div>
  20. <div class="editor-field">
  21. @Html.EditorFor(model => model.FullName)
  22. @Html.ValidationMessageFor(model => model.FullName)
  23. </div>
  24. <div class="editor-label">
  25. @Html.LabelFor(model => model.EmailID)
  26. </div>
  27. <div class="editor-field">
  28. @Html.EditorFor(model => model.EmailID)
  29. @Html.ValidationMessageFor(model => model.EmailID)
  30. </div>
  31. <div class="editor-label">
  32. @Html.LabelFor(model => model.Username)
  33. </div>
  34. <div class="editor-field">
  35. @Html.EditorFor(model => model.Username)
  36. @Html.ValidationMessageFor(model => model.Username)
  37. </div>
  38. <div class="editor-label">
  39. @Html.LabelFor(model => model.Password)
  40. </div>
  41. <div class="editor-field">
  42. @Html.EditorFor(model => model.Password)
  43. @Html.ValidationMessageFor(model => model.Password)
  44. </div>
  45. <div class="editor-label">
  46. @Html.LabelFor(model => model.ConfirmPassword)
  47. </div>
  48. <div class="editor-field">
  49. @Html.EditorFor(model => model.ConfirmPassword)
  50. @Html.ValidationMessageFor(model => model.ConfirmPassword)
  51. </div>
  52. <p>
  53. <input type="submit" value="Create" />
  54. </p>
  55. </fieldset>
  56. }
  57. <div>
  58. @Html.ActionLink("Back to List", "Index")
  59. </div>
  60. @section Scripts {
  61. @Scripts.Render("~/bundles/jqueryval")
  62. }
Step 7: To run the User registration Form.

view in browser

registration

User registration