Step 1: Create Registration Table.
Create table
- CREATE TABLE [dbo].[Registration1](
- [UserID] [int] IDENTITY(1,1) NOT NULL,
- [Username] [varchar](50) NOT NULL,
- [Password] [varchar](50) NOT NULL,
- [FullName] [varchar](100) NOT NULL,
- [EmailID] [varchar](200) NULL,
- PRIMARY KEY CLUSTERED
- (
- [UserID] ASC
- )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
- ) 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.
user.cs
- using System;
- using System.Collections.Generic;
- using System.ComponentModel.DataAnnotations;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
-
- namespace MVCRegistration.Models {
- public class User {
- public int UserID {
- get;
- set;
- }
- [Required(ErrorMessage = "Please provide username", AllowEmptyStrings = false)]
- public string Username {
- get;
- set;
- }
- [Required(ErrorMessage = "Please provide Password", AllowEmptyStrings = false)]
- [DataType(System.ComponentModel.DataAnnotations.DataType.Password)]
- [StringLength(50, MinimumLength = 8, ErrorMessage = "Password must be 8 char long.")]
- public string Password {
- get;
- set;
- }
- [Compare("Password", ErrorMessage = "Confirm password dose not match.")]
- [DataType(System.ComponentModel.DataAnnotations.DataType.Password)]
- public string ConfirmPassword {
- get;
- set;
- }
- [Required(ErrorMessage = "Please provide full name", AllowEmptyStrings = false)]
- public string FullName {
- get;
- set;
- }
-
- [RegularExpression(@
- "^([0-9a-zA-Z]([\+\-_\.][0-9a-zA-Z]+)*)+@(([0-9a-zA-Z][-\w]*[0-9a-zA-Z]*\.)+[a-zA-Z0-9]{2,3})$",
- ErrorMessage = "Please provide valid email id")]
- public string EmailID {
- get;
- set;
- }
-
- }
- }
Step 4: Add Data Enity Model
Step 5: Add UserController.
Usercontroller.cs
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- using MVCRegistration.Models;
-
- namespace MVCRegistration.Controllers {
- public class UserController: Controller {
-
-
-
- public ActionResult Register() {
- return View();
- }
-
- [HttpPost]
- [ValidateAntiForgeryToken]
- public ActionResult Register(Registration1 U) {
- if (ModelState.IsValid) {
- using(RBACEntities dc = new RBACEntities()) {
- dc.Registration1.Add(U);
- dc.SaveChanges();
- ModelState.Clear();
- U = null;
- ViewBag.Message = "Successfully Registration Done";
- }
- }
- return View(U);
- }
-
- }
- }
Step 6: Add View.
Register.cshtml
- @model MVCRegistration.Models.User
-
- @{
- ViewBag.Title = "Register";
- }
-
-
- <h2>Register</h2>
-
- @using (Html.BeginForm()) {
- @Html.ValidationSummary(true)
-
-
- <fieldset>
- <legend>User</legend>
- @Html.AntiForgeryToken()
- @if (ViewBag.Message != null)
- {
-
- <div style="border:solid 1px green">
- @ViewBag.Message
- </div>
- }
-
- <div class="editor-label">
- @Html.LabelFor(model => model.FullName)
- </div>
- <div class="editor-field">
- @Html.EditorFor(model => model.FullName)
- @Html.ValidationMessageFor(model => model.FullName)
- </div>
- <div class="editor-label">
- @Html.LabelFor(model => model.EmailID)
- </div>
- <div class="editor-field">
- @Html.EditorFor(model => model.EmailID)
- @Html.ValidationMessageFor(model => model.EmailID)
- </div>
- <div class="editor-label">
- @Html.LabelFor(model => model.Username)
- </div>
- <div class="editor-field">
- @Html.EditorFor(model => model.Username)
- @Html.ValidationMessageFor(model => model.Username)
- </div>
- <div class="editor-label">
- @Html.LabelFor(model => model.Password)
- </div>
- <div class="editor-field">
- @Html.EditorFor(model => model.Password)
- @Html.ValidationMessageFor(model => model.Password)
- </div>
- <div class="editor-label">
- @Html.LabelFor(model => model.ConfirmPassword)
- </div>
- <div class="editor-field">
- @Html.EditorFor(model => model.ConfirmPassword)
- @Html.ValidationMessageFor(model => model.ConfirmPassword)
- </div>
- <p>
- <input type="submit" value="Create" />
- </p>
- </fieldset>
- }
-
-
- <div>
- @Html.ActionLink("Back to List", "Index")
- </div>
-
- @section Scripts {
- @Scripts.Render("~/bundles/jqueryval")
- }
Step 7: To run the User registration Form.