In this article I will explain with an example, how to insert (save) Hijri Date in SQL Server database table in ASP.Net Core MVC.
Database
I have made use of the following table IslamicEvents with the schema as follows.
Note: You can download the database table SQL by clicking the download link below.
Model
The following Model class consists of two properties.
public class IslamicEventModel
{
public string Event { get; set; }
public string Date { get; set; }
}
Database Context
Once the
Entity Framework is configured and connected to the database table, the Database Context will look as shown below.
using Microsoft.EntityFrameworkCore;
namespace Hijri_Date_Insert_MVC_Core
{
public class DBCtx : DbContext
{
public DBCtx(DbContextOptions<DBCtx> options) : base(options)
{
}
public DbSet<IslamicEventModel> IslamicEvents { get; set; }
}
}
Controller
The Controller consists of following two Action methods.
Action method for handling GET operation
Inside this Action method, simply the View is returned.
Action method for handling POST operation
The Action method for POST operation accepts IslamicEventModel as parameter. The values posted from the Form inside the View are received through this parameter.
Finally, the received values are then inserted into the SQL Server database table using
Entity Framework.
public class HomeController : Controller
{
private DBCtx Context { get; set; }
public HomeController(DBCtx _context)
{
this.Context = _context;
}
public IActionResult Index()
{
return View();
}
[HttpPost]
public IActionResult Index(IslamicEventModel islamicEvent)
{
this.Context.IslamicEvents.Add(islamicEvent);
this.Context.SaveChanges();
return View();
}
}
View
Inside the View, in the very first line the IslamicEventModel class is declared as model for the View.
The View consists of an HTML Form which has been created using the following TagHelpers attributes.
asp-action – Name of the Action. In this case the name is Index.
asp-controller – Name of the Controller. In this case the name is Home.
method – It specifies the Form Method i.e. GET or POST. In this case it will be set to POST.
Inside the HTML Form, there are two TextBox field created for capturing value for Event name and Hijri date and a Submit Button.
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.
Form Submit
When the Submit Button is clicked the form is submitted to the Controllers Action Method.
@model Hijri_Date_Insert_MVC_Core.Models.IslamicEventModel
@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" asp-controller="Home" asp-action="Index">
<div class="container">
<div class="row">
<div class="col-md-12">
<label>Event</label>
<div class="input-group">
<input type="text" asp-for="Event" class="form-control" />
</div>
</div>
<div class="col-md-12">
<label>Date</label>
<div class="input-group">
<input type="text" asp-for="Date" class="form-control" id="txtHijriDate" />
</div>
</div>
<div class="col-md-12">
<br/>
<input type="submit" value="Submit" class="btn btn-primary" />
</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
Date inserted in Table
Downloads