Now let's create a new asp.net core 8 mvc application using c#.net. Afte add a model class file and add the property. This property will help us to get the search data query data and to pass the search result to display in tabular format.
public class EmployeeDetailModel
{
public string SearchText { get; set; }
public List<EmployeeDetail> EmployeeDetailList { get; set; }
}
public class EmployeeDetail
{
public int Id { get; set; }
public string Employee_Name { get; set; }
public string ProjectName { get; set; }
public string Department { get; set; }
public int? Salary { get; set; }
}
So, in above code I have defined a model class and in this model class, I have taken a property of string type and a list of employees to display. Now let's add the controller class file and add an HttpGet method.[HttpGet]
public IActionResult Index()
{
EmployeeDetailModel employeeDetailModel = new EmployeeDetailModel();
employeeDetailModel.EmployeeDetailList = new List<EmployeeDetail>();
return View(employeeDetailModel);
}
In above code I have created the object for the model class file and after that I have created object for list of employees and passed it to the view. @model EmployeeDetailModel
@{
ViewData["Title"] = "Home Page";
}
@using (Html.BeginForm("Index", "Home", FormMethod.Post))
{
<span>
Searh Text: @Html.TextBoxFor(m => m.SearchText, new { @class = "form-control" })
</span>
<br />
<table class="table">
<thead>
<tr>
<th>ID</th>
<th>NAME</th>
<th>PROJECT</th>
<th>DEPARTMENT</th>
<th>SALARY</th>
</tr>
</thead>
<tbody>
@if (Model.EmployeeDetailList.Count > 0)
{
foreach (var item in Model.EmployeeDetailList)
{
<tr>
<td>@item.Id</td>
<td>@item.Employee_Name</td>
<td>@item.ProjectName</td>
<td>@item.Department</td>
<td>@item.Salary</td>
</tr>
}
}
else
{
<tr>
<td>No record to diplay...</td>
</tr>
}
</tbody>
</table>
<input type="submit" value="Search" />
}
In above code I have added the reference of mode class EmployeeDetailModel. After adding the model class reference. In next line I have added form tag, in this form tag I have defined controller name ad action and form method is defined as Post. After that I have taken Html.TextBoxFor control to make user input the search text. At the end code to display of table is written in this if no record is there on that case a user-friendly message has been displayed. A submit button to make the form post to the controller to get the data as per user request. Now nets write the httppost method to perform the search detail.
[HttpPost]
public IActionResult Index(EmployeeDetailModel employeeDetailModel)
{
employeeDetailModel.EmployeeDetailList = new List<EmployeeDetail>();
EmployeeContext employeeContext = new EmployeeContext();
var data = employeeContext.VwEmployees.Where(m =>
m.EmployeeName.Contains(employeeDetailModel.SearchText) ||
m.Department.Contains(employeeDetailModel.SearchText) ||
m.ProjectName.Contains(employeeDetailModel.SearchText));
foreach (var item in data)
{
employeeDetailModel.EmployeeDetailList.Add(new EmployeeDetail
{
Id = item.Id,
Employee_Name = item.EmployeeName,
ProjectName = item.ProjectName,
Department = item.Department,
Salary = item.Salary
});
}
return View(employeeDetailModel);
}
Here in httpost method, I have passed the model class name a parameter. This will help us to capture the user entered search value at controller end. After that I have created object for employeelist. This we use to store the fetched employee list and pass it to view. To get the search result I have created object of DB context and used LINQ query to get the employee list. Here I have used searched the passed value with different fields with contains. After getting the value it has been stored in the list. Now we have done, lets run the code and check the output.
In above code we can see that the there is no record. Now let's add some text search and click on search text.
As we click on search the break point at controller end we can see the added search text.
Here we can see the user entered detail we are able to get at controller end. Now ones proceed further we will be able to get the result as per user entered value.