In this article I will explain with an example, how to insert (save) Hijri Date in SQL Server Database table in ASP.Net Core Razor Pages.
 
 
Database
I have made use of the following table IslamicEvents with the schema as follows.
ASP.Core Razor Pages: Insert (Save) Hijri Date in Database
 
Note: You can download the database table SQL by clicking the download link below.
         Download SQL file
 
 
Model
The following Model class consists of two properties.
public class IslamicEventModel
{
    public string Event { getset; }
    public string Date { getset; }
}
 
 
Database Context
Once the Entity Framework is configured and connected to the database table, the Database Context will look as shown below.
Note: For beginners in ASP.Net Core Razor Pages and Entity Framework, please refer my article ASP.Net Core Razor Pages: Simple Entity Framework Tutorial with example. It covers all the information needed for connecting and configuring Entity Framework with ASP.Net Core.
 
using Microsoft.EntityFrameworkCore;
 
namespace Hijri_Date_Insert_Core_Razor
{
    public class DBCtx : DbContext
    {
        public DBCtx(DbContextOptions<DBCtx> options) : base(options)
        {
        }
 
        public DbSet<IslamicEventModel> IslamicEvents { get; set; }
    }
}
 
 
Razor PageModel (Code-Behind)
The PageModel consists of the following two Handler methods.
Handler method for handling GET operation
This Handler method is left empty as it is not required.
 
Handler method for handling Button Click and POST operation
This Handler method for POST operation accepts IslamicEventModel as parameter. The values posted from the Form inside the Razor Page are received through this parameter.
Finally, the received values are then inserted into the SQL Server database table using Entity Framework.
public class IndexModel : PageModel
{
    private DBCtx Context { get; set; }
    public IndexModel(DBCtx _context)
    {
        this.Context = _context;
    }
 
    public void OnGet()
    {
    }
 
    public void OnPostSubmit(IslamicEventModel islamicEvent)
    {
        this.Context.IslamicEvents.Add(islamicEvent);
        this.Context.SaveChanges();
    }
}
 
 
Razor Page (HTML)
Inside the Razor Page, the ASP.Net TagHelpers is inherited.
The HTML of Razor Page consists of an HTML Form consisting of two TextBoxes field created for capturing value for Event name and Hijri date and a Submit Button.
The Submit Button has been set with the POST Handler method using the asp-page-handler attribute.
Note: In the Razor PageModel, the Handler method name is OnPostSubmit but here it will be specified as Submit when calling from the Razor HTML Page.
 
Bootstrap Hijri DatePicker plugin implementation
Inside the View,the following CSS files are inherited.
1. bootstrap.min.css
2. bootstrap-datetimepicker.min.css
 
Then, the following JS scripts are inherited.
1. jquery.min.js
2. moment.min.js
3. moment-hijri.js
4. bootstrap-hijri-datetimepicker.js
Inside the jQuery document ready event handler, the HijriDate TextBox has been applied with the Bootstrap Hijri DatePicker plugin.
Note: For more details on how to implement Bootstrap Hijri DatePicker plugin, please refer my article, ASP.Net Core razor Pages: Implement Hijri (Islamic) Calendar.
 
Form Submit
When the Submit Button is clicked, the form is submitted to the Handler Method.
@page
@model Hijri_Date_Insert_Core_Razor.Pages.IndexModel
@addTagHelper*, Microsoft.AspNetCore.Mvc.TagHelpers
@{
    Layout = null;
}
 
<!DOCTYPE html>
 
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <form method="post">
        <div class="container">
            <div class="row">
                <div class="col-md-12">
                    <label>Event</label>
                    <div class="input-group">
                        <input type="text" name="Event" class="form-control" />
                    </div>
                </div>
                <div class="col-md-12">
                    <label>Date</label>
                    <div class="input-group">
                        <input type="text" name="Date" class="form-control" id="txtHijriDate" />
                    </div>
                </div>
                <div class="col-md-12">
                    <br />
                    <input type="submit" value="Submit" class="btn btn-primary" asp-page-handler="Submit" />
                </div>
            </div>
        </div>
    </form>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet" />
    <link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.17.47/css/bootstrap-datetimepicker.min.css" rel="stylesheet" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.1/moment.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/moment-hijri@2.1.0/moment-hijri.js"></script>
    <script src="~/assets/js/bootstrap-hijri-datetimepicker.js"></script>
    <script type="text/javascript">
        $(function () {
            $("[id*=txtHijriDate]").hijriDatePicker();
        });
    </script>
</body>
</html>
 
 
Screenshots
The Form
ASP.Core Razor Pages: Insert (Save) Hijri Date in Database
 
Date inserted in Table
ASP.Core Razor Pages: Insert (Save) Hijri Date in Database
 
 
Downloads