Introduction
In a client requirement, I needed to create a page where two forms or models exist in a single view (page), like login and registration in the same single view.
Description
To fulfill this requirement, I used MVC with Entity Framework and SQL Server. For more details about MVC, go through my previous articles and blogs.
On the registration page, the user will register as new and on the login page, existing users will log in with their credentials. These two functionalities need to be implemented in the same view using MVC facility. The other script related part is added to show the message for successful or ivalid credentials during registration and login operations.
Source Code
Steps to be followed.
Step 1
Create a table named Users.
- CREATE TABLE [dbo].[Users](
- [UserID] [int] IDENTITY(1,1) NOT NULL,
- [Username] [varchar](50) NULL,
- [Password] [varchar](50) NULL,
- [FullName] [varchar](150) 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]
- GO
I have added entity data model named "Satyadatabasemodel.edmx" . After creating the Data model, we have to modify our generated entity (table) for applying the validation to the required fields. Here, we need to modify the User.cs file.
Code Ref
- namespace SatyaprakashMultimodels
- {
- using System;
- using System.Collections.Generic;
- using System.ComponentModel.DataAnnotations;
- public partial class User
- {
- public int UserID { get; set; }
- [Required]
- public string Username { get; set; }
- [Required]
- [DataType(System.ComponentModel.DataAnnotations.DataType.Password)]
- public string Password { get; set; }
- [Required]
- public string FullName { get; set; }
- }
- }
Code Description
Here, all the fields are put with the required attribute for validation purposes.
Step 3
I need a ViewModel for getting the data as a single entity. Here, I created a class file named "SignIn.cs" in the ViewModel folder.
Code Ref
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.ComponentModel.DataAnnotations;
- namespace SatyaprakashMultimodels.ViewModel
- {
- public class SignIn
- {
- public User User { get; set; }
- public Login Login { get; set; }
- }
- public class Login
- {
- [Required]
- public string UserName { get; set; }
- [Required]
- [DataType(System.ComponentModel.DataAnnotations.DataType.Password)]
- public string Password { get; set; }
- }
- }
I used two classes, SignIn and Login, for new user registration and for logging the existing users in respectively.
Step 4
Then, I have added one controller action method named "LoginRegister" to the HomeController.cs file.
Code Ref
- using SatyaprakashMultimodels.ViewModel;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- namespace SatyaprakashMultimodels.Controllers
- {
- public class HomeController : Controller
- {
- public ActionResult Index()
- {
- return View();
- }
- public ActionResult LoginRegister(SignIn lr)
- {
- return View(new SignIn { Login = new Login(), User = new User() });
- }
- [HttpPost]
- public ActionResult Login(SignIn l)
- {
- string message = "";
- if (ModelState.IsValid)
- {
- using (CrystalGranite2016Entities1 dc = new CrystalGranite2016Entities1())
- {
- var v = dc.Users.Where(a => a.Username.Equals(l.Login.UserName) && a.Password.Equals(l.Login.Password)).FirstOrDefault();
- if (v != null)
- {
- message = "Login Success";
- return RedirectToAction("Success", new { message = message });
- }
- else
- {
- message = "login failed";
- }
- }
- }
- ViewBag.Message = message;
- return View("LoginRegister", l);
- }
- [HttpPost]
- public ActionResult Register(SignIn r)
- {
- string message = "";
- if (ModelState.IsValid)
- {
- using (CrystalGranite2016Entities1 dc = new CrystalGranite2016Entities1())
- {
- var v = dc.Users.Where(a => a.Username.Equals(r.User.Username)).FirstOrDefault();
- if (v == null)
- {
- dc.Users.Add(r.User);
- dc.SaveChanges();
- message = "Successfully Registered";
- return RedirectToAction("Success", new { message = message });
- }
- else
- {
- message = "Username no available";
- }
- }
- }
- ViewBag.Message = message;
- return View("LoginRegister", r);
- }
- public ActionResult Success(string message)
- {
- ViewBag.Message = message;
- return View();
- }
- public ActionResult About()
- {
- ViewBag.Message = "Your application description page.";
- return View();
- }
- public ActionResult Contact()
- {
- ViewBag.Message = "Your contact page.";
- return View();
- }
- }
- }
Code Description
This Action method is used to render one view for both Login & New User Registration.
- public ActionResult LoginRegister(SignIn lr)
- {
- return View(new SignIn { Login = new Login(), User = new User() });
- }
- [HttpPost]
- public ActionResult Login(SignIn l)
- {
- string message = "";
- if (ModelState.IsValid)
- {
- using (CrystalGranite2016Entities1 dc = new CrystalGranite2016Entities1())
- {
- var v = dc.Users.Where(a => a.Username.Equals(l.Login.UserName) && a.Password.Equals(l.Login.Password)).FirstOrDefault();
- if (v != null)
- {
- message = "Login Success";
- return RedirectToAction("Success", new { message = message });
- }
- else
- {
- message = "login failed";
- }
- }
- }
- ViewBag.Message = message;
- return View("LoginRegister", l);
- }







Tin Ho QuangPosted Nov 25, 2018, 11:57 PM
Thank you for the useful article. BTW may I know why do you use "partial type" in "public partial class User". Thanks
Joe WilsonPosted Mar 31, 2018, 5:17 AM
Thank you for sharing it.
Bhavesh JadavPosted Mar 29, 2018, 6:46 AM
Very good explanation with example which is simple and easy to understand. Thanks to share.