Inserting Data into SQL Server Database Using ASP.NET Core 3 Razor Pages

Introduction

 
This article is a continuation of the article, “Creating Page Model And Performing CRUD Operations In ASP.NET Core 3 Razor Pages”. In this article, we will be creating a razor page to implement the create functionality of crud operations. 
 

Creating a Razor page

 
First, we have to create a new razor page. In order to create a new razor page, right-click on the BookList folder. Click on the Add option. Then click on the razor page option to open the “Add New Scaffolded Item” window. Select the Razor Page option, then click on Add button to add a new razor page.
 
Inserting Data Into SQL Server Database Using ASP.NET Core 3 Razor Pages 
 
Inside the 'Add Razor Page' window, write the name of the razor page. Select options, then Generate Page Model class and use a layout page. Click on the 'Add' button. Then, the create razor page will be created.
 
Inserting Data Into SQL Server Database Using ASP.NET Core 3 Razor Pages 
 
When the page loads, we want to display text boxes so that the user can enter a book name and author name.
 
So we need Application Db Context in order to post data to the database when the user clicks the create button. Declare an object of type Application Db Context. Now, create a constructor and pass Application Db Context object as a parameter to the constructor.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Threading.Tasks;  
  5. using BookList.Model;  
  6. using Microsoft.AspNetCore.Mvc;  
  7. using Microsoft.AspNetCore.Mvc.RazorPages;  
  8.   
  9. namespace BookList  
  10. {  
  11.     public class CreateModel : PageModel  
  12.     {  
  13.         private readonly ApplicationDbContext _db;  
  14.   
  15.         public CreateModel(ApplicationDbContext db)  
  16.         {  
  17.             _db = db;  
  18.         }  
  19.   
  20.     }  
  21. }  
Inside the get handler method, we need to display text boxes to write the author's name and book name.
 
The model which we will be using is going to be the Book model. Inside OnGet handler method, we do not have to write that we are passing this empty book object since it does that automatically.
 
Therefore, inside the create view, we will be able to access the book object and display labels as well as text boxes. Inside the Book model let’s add a new property called ISBN along with getter and setter.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel.DataAnnotations;  
  4. using System.Linq;  
  5. using System.Threading.Tasks;  
  6.   
  7. namespace BookList.Model  
  8. {  
  9.     public class Book  
  10.     {  
  11.         [Key]  
  12.         public int Id { getset; }  
  13.   
  14.         [Required]  
  15.         public string Name { getset; }  
  16.   
  17.         public string Author { getset; }  
  18.   
  19.         public string ISBN { getset; }  
  20.   
  21.     }  
  22. }  
Since we have modified the Book model, we have to add a new migration.
 
To add migration, go to Tools, Click on Nuget Package Manager and then click on Package Manager Console. Inside Package Manager Console, type add-migration AddISBNToBookModel.
 
Inserting Data Into SQL Server Database Using ASP.NET Core 3 Razor Pages
 
To make changes inside the SQL server database, type command update-database. 
 
This is how a new column will be added inside the table in order to changes to the model that we already added. Next, we need to change the Ind.cshml file accordingly.
 
You must design a create razor page inside the create.cshtml file.
  1. @page  
  2. @model BookList.CreateModel  
  3.   
  4. <br />  
  5. <h2 class="text-info">Create New Book</h2>  
  6. <br />  
  7.   
  8. <div class="border container" style="padding:30px;">  
  9.     <form method="post">  
  10.         <div class="form-group row">  
  11.             <div class="col-4">  
  12.                 <label asp-for="Book.Name"></label>  
  13.             </div>  
  14.             <div class="col-6">  
  15.                 <input asp-for="Book.Name" class="form-control" />  
  16.             </div>  
  17.         </div>  
  18.   
  19.         <div class="form-group row">  
  20.             <div class="col-4">  
  21.                 <label asp-for="Book.Author"></label>  
  22.             </div>  
  23.             <div class="col-6">  
  24.                 <input asp-for="Book.Author" class="form-control" />  
  25.             </div>  
  26.         </div>  
  27.   
  28.         <div class="form-group row">  
  29.             <div class="col-4">  
  30.                 <label asp-for="Book.ISBN"></label>  
  31.             </div>  
  32.             <div class="col-6">  
  33.                 <input asp-for="Book.ISBN" class="form-control" />  
  34.             </div>  
  35.         </div>  
  36.         <div class="form-group row">  
  37.             <div class="col-3 offset-4">  
  38.                 <input type="submit" value="Create" class="btn btn-primary form-control" />  
  39.             </div>  
  40.             <div class="col-3">  
  41.                 <a asp-page="Ind" class="btn btn-success form-control">Back to List</a>  
  42.             </div>  
  43.         </div>  
  44.     </form>  
  45. </div>   
We need to create a post handler for create button since when we hit the create button, we are posting data back to the page handler. Now we need to add a post handler so that whenever we submit the data from the page, it's submitted to the database.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Threading.Tasks;  
  5. using BookList.Model;  
  6. using Microsoft.AspNetCore.Mvc;  
  7. using Microsoft.AspNetCore.Mvc.RazorPages;  
  8.   
  9. namespace BookList  
  10. {  
  11.     public class CreateModel : PageModel  
  12.     {  
  13.         private readonly ApplicationDbContext _db;  
  14.   
  15.         public CreateModel(ApplicationDbContext db)  
  16.         {  
  17.             _db = db;  
  18.         }  
  19.   
  20.         [BindProperty]  
  21.         public Book Book { getset; }  
  22.         public void OnGet()  
  23.         {  
  24.   
  25.         }  
  26.   
  27.         public async Task<IActionResult> OnPost()  
  28.         {  
  29.             if(ModelState.IsValid)  
  30.             {  
  31.                 await _db.Book.AddAsync(Book);  
  32.                 await _db.SaveChangesAsync();  
  33.                 return RedirectToPage("Ind");  
  34.   
  35.             }  
  36.             else  
  37.             {  
  38.                 return Page();  
  39.             }  
  40.         }  
  41.     }  
  42. }  
Inside the Create.cshtml.cs file, OnGet handler is available but when we will hit the submit button, we will be posting data so we need a post handler named OnPost to retrieve the submitted data.
 
The Task will be of type IActionResult because we will be redirecting to a new page. After we add the book, we need to save changes to the database.
 
After running the application on the browser, the new razor view will look like shown in the following image.
 
Inserting Data Into SQL Server Database Using ASP.NET Core 3 Razor Pages 
 
After inserting a record inside the database, the table would look like the below image.
 
Inserting Data Into SQL Server Database Using ASP.NET Core 3 Razor Pages 
 
And the book table inside the SQL server database will be updated with more rows.
 
Inserting Data Into SQL Server Database Using ASP.NET Core 3 Razor Pages 
 

Summary

 
In this article, we have created a new property named ISBN inside Book model class, then we updated the database table by adding and updating the migrations command. We created a razor view page and added a post handler. This way, whenever we submit the data from the page, it should be submitted to the database. I will be discussing the remaining functionalities of crud operation in the upcoming articles.