In this article i will show you how you can search or find data between two dates in asp.net core 6/ MVC using MS Sql database using c# with entity framework. In this asp.net core 6 tutorial i will user entity framework to access data and filter data using linq in c#.net.
Other articles: How To Read / Access Email Setting In Appsettings.json In Asp.Net Core 6 in Controller Using C#.Net, Search And Display Data In Table/Tabular Format in Asp.net Core 6/MVC using C# (Ms Sql Server), Display Data In Table/Tabular Format In Asp Net Core 6/MVC Using C#.Net (Ms Sql Database), Asp.net Core 6: Ajax Login Form Using jQuery, C# Validate From Ms Sql Server Database.
Now for this article first we will create a new asp.net core 6/MVC application. Now we will user Scamffold commend to make entity object.
Scaffold-DbContext
"Server=LAPTOP-DCE5GAKD\SQLEXPRESS;Database=TestDB;Trusted_Connection=True;"
Microsoft.EntityFrameworkCore.SqlServer -OutputDir data -force |
Now we will get all our table entity in data folder.
Now check the data present in the table. We will apply code to get the data from data base as per passed date.
Now we will create a model class in this we will define the properties into it.
using
System; using
System.Collections.Generic; using
System.Linq; using
System.Threading.Tasks;
namespace Project.Models {
public class EmployeeModel
{ public List<EmployeeData> EmployeeDataList { get; set; }
}
public class EmployeeData
{ public int Id { get; set; } public string EmployeeName { get; set; } public string Department { get; set; } public string Location { get; set; } public DateTime CreatedDate { get; set; }
} } |
[HttpGet] public IActionResult Index() { TestDBContext testDBContext = new TestDBContext(); EmployeeModel employeeModel = new EmployeeModel(); employeeModel.EmployeeDataList = new List<EmployeeData>(); //DateTime
date = new DateTime(); DateTime fromdate = DateTime.Now.AddDays(-45); DateTime todate = DateTime.Now; var data = testDBContext.Employees.Where(m => m.CreatedDate
>= fromdate && m.CreatedDate <= todate); foreach (var item in data) {
employeeModel.EmployeeDataList.Add(new EmployeeData { Id = item.Id, CreatedDate =
item.CreatedDate, Department =
item.EmployeeName, EmployeeName =
item.EmployeeName, Location = item.Location }); } return View(employeeModel); } |
@model Project.Models.EmployeeModel @{
ViewData["Title"] = "Home Page"; } @using (Html.BeginForm("Index", "Home",
FormMethod.Post, new { @id
= "formxmldata",
@enctype = "multipart/form-data" })) {
<table class="table
table-bordered"> <thead> <tr> <th>Id</th> <th>Employee Name</th> <th>Department</th> <th>Location</th> <th>Created Date</th> </tr> </thead> <tbody> @foreach (var item in
Model.EmployeeDataList) { <tr> <td>@item.Id</td> <td>@item.EmployeeName</td> <td>@item.Department</td> <td>@item.Location</td> <td>@item.CreatedDate</td> </tr> } </tbody>
</table> } |
0 comments:
Please let me know your view