Introduction
In this article I will explain how to create a user registration form in ASP.NET Core MVC application. An MVC application consists of model, view and controller. In this application I will create and scaffold a model to work with database and store user details in SQL Server database. The controller will be responsible for selecting a view to be displayed to the user and provides the necessary data model to store data on to the SQL Server database. The controller manages how the application would respond to the post request made by the user.
Creating ASP.NET Core MVC web application
Before writing the coding part of ASP.NET Core MVC application, first we need to create a table in SQL Server database. The table can be created with the help of following SQL query.
- create table UserRegistration
- (
- UserId int not null primary key identity(1,1),
- Username nvarchar(150),
- Pwd nvarchar(100),
- Confirmpwd nvarchar(100),
- Email nvarchar(150),
- Gender char,
- MaritalStatus nvarchar(100)
- );
Now we will create a new ASP.NET Core MVC application as follows.

Here we will select ASP.NET Core Web application, provide project name and select model-view-controller as web application template.
Now we will be adding a connection string inside appsettings.json to connect to the SQL Server database.
- {
- "ConnectionStrings": {
- "Myconnection": "Data Source=DESKTOP-QGAEHNO\\SQLEXPRESS;Initial Catalog=profiledb;Integrated Security=True"
- },
- "Logging": {
- "LogLevel": {
- "Default": "Information",
- "Microsoft": "Warning",
- "Microsoft.Hosting.Lifetime": "Information"
- }
- },
- "AllowedHosts": "*"
- }
The next step is to add a class named user inside models folder. Inside user class, we will be defining properties required to interact with the registration form.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- using System.ComponentModel.DataAnnotations;
- namespace UserReg.Models
- {
- public class User
- {
- [Key]
- public int UserId { get; set; }
- [Required(ErrorMessage ="Please Enter Username..")]
- [Display(Name = "UserName")]
- public string Username { get; set; }
- [Required(ErrorMessage ="Please Enter Password...")]
- [DataType(DataType.Password)]
- [Display(Name ="Password")]
- public string Pwd { get; set; }
- [Required(ErrorMessage = "Please Enter the Confirm Password...")]
- [DataType(DataType.Password)]
- [Display(Name = "Confirm Password")]
- [Compare("Pwd")]
- public string Confirmpwd { get; set; }
- [Required(ErrorMessage = "Please Enter Email...")]
- [Display(Name = "Email")]
- public string Email { get; set; }
- [Required(ErrorMessage = "Select the Gender...")]
- [Display(Name = "Gender")]
- public string Gender { get; set; }
- [Required(ErrorMessage = "Select the Marital Status...")]
- [Display(Name = "Marital Status")]
- public string MaritalStatus { get; set; }
- }
- }
Here user id is a primary key and to each and every property the required validation has been assigned so that if a user tries to click the submit button without filling the registration form, the application will throw an error.
Now we will define another class named ApplicationUser to add a DbContext to connect and query the database.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- using Microsoft.EntityFrameworkCore;
- namespace UserReg.Models
- {
- public class ApplicationUser:DbContext
- {
- public ApplicationUser(DbContextOptions<ApplicationUser> options): base(options)
- {
- }
- public DbSet<User> UserRegistration { get; set; }
- }
- }
Here we are adding the DbSet property to query the database.
Now within startup.cs class, we need to add services for application user class and connection string inside configureServices method.
- public void ConfigureServices(IServiceCollection services)
- {
- services.AddDbContext<ApplicationUser>(options => options.UseSqlServer(Configuration.GetConnectionString("Myconnection")));
- services.AddControllersWithViews();
- }




JAINAM SWAMIPosted May 1, 2023, 9:50 AM
What do i write in startup.cs file it is not opening
Deepak SharmaPosted Jul 1, 2022, 12:53 PM
Can you create sign up in .net framework according to coding, so that, a fresher employee learn Sign up Page without any problems.
Victor RussoPosted Jul 30, 2020, 10:00 PM
Great Tutorial. How do I get the create.cshtml to view in the browser?