Hi zeeshanpas,
To filter record you need to fist create DateTime variable from the Date TextBox and Time DropDownList.
Then use the DateTime to fetch the record.
Check this example. Now please take its reference and correct your code.
Database
For this example I have used of Northwind database that you can download using the link given below.
Download Northwind Database
Controller
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
using (NorthwindEntities entities = new NorthwindEntities())
{
List<Employee> employees = (from e in entities.Employees
select e).ToList();
return View(employees);
}
}
[HttpPost]
public ActionResult Index(string date, string time)
{
TimeSpan timeSpan = DateTime.ParseExact(time, "h:mm tt", System.Globalization.CultureInfo.InvariantCulture).TimeOfDay;
DateTime dateTime = Convert.ToDateTime(date).Add(timeSpan);
using (NorthwindEntities entities = new NorthwindEntities())
{
List<Employee> employees = (from c in entities.Employees
where c.BirthDate == dateTime
select c).ToList();
return View(employees);
}
}
}
View
@model List<WebGrid_EF_MVC.Employee>
@{
Layout = null;
WebGrid webGrid = new WebGrid(source: Model, canSort: false, rowsPerPage: 5);
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.24/themes/smoothness/jquery-ui.css" />
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.24/jquery-ui.min.js"></script>
<script type="text/javascript">
$(function () {
$("#txtDate").datepicker({ minDate: new Date() });
});
</script>
</head>
<body>
@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { @Id = "WebGridForm" }))
{
<table>
<tr>
<td>Date: </td>
<td>@Html.TextBox("date", "", new { @id = "txtDate" })</td>
</tr>
<tr>
<td>Time: </td>
<td>
@Html.DropDownList("time", new List<SelectListItem> {
new SelectListItem{Text="1:00 AM", Value="1:00 AM"},
new SelectListItem{Text="2:00 AM", Value="2:00 AM"},
new SelectListItem{Text="3:00 AM", Value="3:00 AM"},
new SelectListItem{Text="4:00 AM", Value="4:00 AM"},
new SelectListItem{Text="5:00 AM", Value="5:00 AM"},
new SelectListItem{Text="6:00 AM", Value="6:00 AM"},
new SelectListItem{Text="7:00 AM", Value="7:00 AM"},
new SelectListItem{Text="8:00 AM", Value="8:00 AM"},
new SelectListItem{Text="9:00 AM", Value="9:00 AM"},
new SelectListItem{Text="10:00 AM", Value="10:00 AM"},
new SelectListItem{Text="11:00 AM", Value="11:00 AM"},
new SelectListItem{Text="12:00 AM", Value="12:00 AM"},
}, "Select Time", new { @id = "ddlTimes" })
</td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Submit" /></td>
</tr>
</table>
}
<hr />
@webGrid.GetHtml(
htmlAttributes: new { @id = "WebGrid", @class = "Grid" },
columns: webGrid.Columns(
webGrid.Column("EmployeeId", "Id"),
webGrid.Column("FirstName", "Name"),
webGrid.Column("City", "City"),
webGrid.Column("Country", "Country")))
</body>
</html>
Screenshot
Note: In the database table there is no record for the selected DateTime. So the record is empty after filter.