I am trying to filter my webgrid based on the two dates. Its working fine for first page but second page it losts filter and generrates original list even it ihas filtered in the list collection. I wanted to maintain filter till it shows all pages that satisfy
the filter. how can i achieve that?
Filter based on following two dates
<div id="right">
@using (Html.BeginForm("index", "GroupInfo", FormMethod.Post))
{
<h2>Filter groups on Expiry Date:</h2>
Session["Range1"]= Convert.ToDateTime(Request.Form["range1"]);
Session["Range2"] = Convert.ToDateTime(Request.Form["range2"]);
DateTime rangeDate1=(DateTime)Session["Range1"];
DateTime rangeDate2 = (DateTime)Session["Range2"];
string rangeButton=Request.Form["button1"];
List<GroupInfo> FilterBatch = new List<GroupInfo>();
if (rangeDate1 != null && rangeDate2 != null &&rangeButton!=null)
{
if (rangeDate1 > rangeDate2)
{
ModelState.AddModelError("", "Start date must be less than or equal to end date");
return View(_repository.GroupInfoListAll());
}
foreach (var item in Groups)
{
if (item.RenewalDate >= rangeDate1 && item.RenewalDate<= rangeDate2)
{
FilterBatch.Add(item);
}
}
Session["FilterBatch"] = FilterBatch;
if (FilterBatch.Count() == 0)
{
// ModelState.AddModelError("", "No gorups expiring in the given range");
return View(_repository.GroupInfoListAll());
}
else
{
ViewBag.checkAll = "true"; //setting all groups checked on filter
}
return View(FilterBatch);
Firstly, from your code we could find you save two dates in Session variables then you retrieve dates from Session variables and convert them to DateTime. Session["Range1"] and Session["Range2"] just work as intermediate variables, if you do not use these
Session variables in other controller actions, it will be unnecessary to store dates in Session variable.
Secondly, you could refer to the following links to filter and page WebGrid.
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
Member
9 Points
26 Posts
Filter in Web grid Asp.net MVC
Sep 01, 2015 02:10 PM|92patilshailesh|LINK
Hello,
I am trying to filter my webgrid based on the two dates. Its working fine for first page but second page it losts filter and generrates original list even it ihas filtered in the list collection. I wanted to maintain filter till it shows all pages that satisfy the filter. how can i achieve that?
Filter based on following two dates
<div id="right">
@using (Html.BeginForm("index", "GroupInfo", FormMethod.Post))
{
<h2>Filter groups on Expiry Date:</h2>
<label style="width:150px;">Enter start date:</label><input type="text" name="range1" id="datepicker1" value="mm/dd/yyyy" required="required" /><br />
<label style="width:150px;">Enter end date:</label><input type="text" name="range2" id="datepicker2" value="mm/dd/yyyy" required="required" /><br />
<input type="submit" name="button1" value="Ok" />
}
</div>
in my controller I am doing:
Session["Range1"]= Convert.ToDateTime(Request.Form["range1"]);
Session["Range2"] = Convert.ToDateTime(Request.Form["range2"]);
DateTime rangeDate1=(DateTime)Session["Range1"];
DateTime rangeDate2 = (DateTime)Session["Range2"];
string rangeButton=Request.Form["button1"];
List<GroupInfo> FilterBatch = new List<GroupInfo>();
if (rangeDate1 != null && rangeDate2 != null &&rangeButton!=null)
{
if (rangeDate1 > rangeDate2)
{
ModelState.AddModelError("", "Start date must be less than or equal to end date");
return View(_repository.GroupInfoListAll());
}
foreach (var item in Groups)
{
if (item.RenewalDate >= rangeDate1 && item.RenewalDate<= rangeDate2)
{
FilterBatch.Add(item);
}
}
Session["FilterBatch"] = FilterBatch;
if (FilterBatch.Count() == 0)
{
// ModelState.AddModelError("", "No gorups expiring in the given range");
return View(_repository.GroupInfoListAll());
}
else
{
ViewBag.checkAll = "true"; //setting all groups checked on filter
}
return View(FilterBatch);
}
can somebody please help?
filter WebGrid mvc asp.net csharp
All-Star
40535 Points
6233 Posts
Microsoft
Re: Filter in Web grid Asp.net MVC
Sep 02, 2015 01:53 AM|Fei Han - MSFT|LINK
Hi 92patilshailesh,
Firstly, from your code we could find you save two dates in Session variables then you retrieve dates from Session variables and convert them to DateTime. Session["Range1"] and Session["Range2"] just work as intermediate variables, if you do not use these Session variables in other controller actions, it will be unnecessary to store dates in Session variable.
Secondly, you could refer to the following links to filter and page WebGrid.
http://www.mikesdotnetting.com/article/180/displaying-search-results-in-a-webgrid
http://www.c-sharpcorner.com/UploadFile/4d9083/filter-webgrid-with-cascading-dropdownlist-along-with-paging/
Best Regards,
Fei Han