I am trying to make a search filter by date but i get an error.
This is my Search method:
public ActionResult Search(DateTime StartDate,DateTime EndDate)
{
var result = (
from r in context.Rooms
from v in context.Reservations
.Where(a =>a.Room_ID == r.RoomID
&&
!(
StartDate >= a.Data_Check_out ||
EndDate <= a.Data_Check_in
)
&& a.Cancel == false
).DefaultIfEmpty()
where v.ReservationID == null
select r
);
return View(result);
}
And here is my View:
@model IEnumerable<MvcApplication4.Models.Room>
@using (Html.BeginForm("Search", "Room", FormMethod.Get))
{
@Html.TextBox("StartDate")
<input type="submit" value="Search" />
@Html.TextBox("EndDate")
<input type="submit" value="Search" /> }
@foreach (var x in Model)
{
<div class="item">
<h1>@x.Room_Type.Room_Type1</h1>
</div>}`
I get the following error:
The parameters dictionary contains a null entry for parameter ‘StartDate’ of non-nullable type ‘System.DateTime’ for method ‘System.Web.Mvc.ActionResult Search(System.DateTime, System.DateTime)’ in ‘MvcApplication4.Controllers.RoomController’. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.
Parameter name: parameters`
Are you are passing the parameters in url? If not then its better to use model in your controller to fetch value. This error is coming because controller is not able to get the parameter values.