Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC Filtering results in a list/grid

Tags:

asp.net-mvc

For some reason I'm stuck on this. I need to filter results from a View based on a DropDownList in the same view. The basic idea is this: I have a list of providers that belong to various partners, but the provider list contains ALL the providers together (for all partners). I need to be able to display the providers by partner when someone wants to see just that partner (otherwise, the default listing will be ALL providers). My view currently is the "default" (showing all), but for some reason Im sitting here staring at the monitor (for the last 2 hours!) trying to figure out how to filter these results.

Any suggestions where to start/how to do it?!

like image 657
SlackerCoder Avatar asked Dec 29 '09 21:12

SlackerCoder


People also ask

How are result filters implemented in MVC?

Mvc. FilterAttribute class. If you want to implement a particular type of filter, then you need to create a class that inherits from the base Filter class and implements one or more of the IAuthorizationFilter , IActionFilter , IResultFilter , or IExceptionFilter interfaces.

When filters are executed in MVC?

Action filters are executed before or after an action is executed. The IActionFilter interface is used to create an Action Filter which provides two methods OnActionExecuting and OnActionExecuted which will be executed before or after an action is executed respectively.

Which filter will be executed first in ASP.NET MVC?

as you can see from the below diagram, as soon as the controller starts execution through Action Invoker, Authentication and authorization filters are the very first filters to be triggered, followed by model binding which maps request and route data to action parameters.


4 Answers

EDIT: If you want to do this with jQuery and AJAX (which will provide a better user experience because only the subdivisions list will refresh), see this tutorial.

If I understand correctly, you basically want to do a WebForms-style postback.

Let's say you have a control with countries and country subdivisions (e.g. states, provinces, etc). When the country changes, you want the appropriate subdivisions to display.

So this would be view:

<% using (Html.BeginForm()) { %>
    <%=Html.DropDownList("Address.CountryId", new SelectList(Country.GetAll(), "Id", "Name"), new { onchange = "this.form.submit();" })%>
    <%=Html.DropDownList("Address.CountrySubdivisionId", new SelectList(CountrySubDivision.GetByCountryId(Model.CountryId), "Id", "Name"))%>
    <input type="submit" name="btnSubmit" value="Submit"/>
<%} %>

This is the key to getting the dependent list to filter:

new { onchange = "this.form.submit();" }

And in the controller, you'd have something like this:

    [AcceptVerbs(HttpVerbs.Post)]
    public ViewResult Index(string btnSubmit)
    {
        if (btnSubmit == null)
        {
            // return the view displayed upon GET
        }
        else
        {
            // process the submitted data
        }
    }

In the above code, if the form submission was triggered by changing the value in a dropdown, btnSubmit will be null. Thus, the action you are POSTing to can tell whether or not the user meant to finalize her changes.

like image 196
Josh Kodroff Avatar answered Oct 13 '22 10:10

Josh Kodroff


To add upon the earlier answers.

To create a drop down (in ASP .NET MVC 3) I did the following:

Add code to Index.cshtml

@using (Html.BeginForm())
{      
@Html.DropDownList("EmployeeId", (SelectList)ViewData["EmployeeId"])     
 <input type="submit" name="btnSubmit" value="Submit"/> 
}

Add code to YourModelNameController.cs in the default ActionResult for Index()

public ActionResult Index()
{

    //create a selectlist
        var employeeList = from el in db.Employee select el;
        ViewData["EmployeeId"] = new SelectList(employeeList, "EmployeeId", "TmName");

        return View(modelName);
    }
like image 41
John M Avatar answered Oct 13 '22 12:10

John M


There are many ways to skin this cat. Here's one.

Enclose your DropDownList in a form with METHOD=GET.

<form action="" method="get">
  <select name="provider">
    <option>1</option>
    <!-- etc -->
  </select>
</form>

Then, in you controller, filter based on the value of provider that was passed in. Remember to treat it as a Nullable parameter so that you can have some kind of behavior when it's empty.

Without posting some of your current code, it's tough to get much more specific than that.

like image 40
Portman Avatar answered Oct 13 '22 10:10

Portman


Let's assume that you're probably passing a model to the view and that model is a list or IEnummerable of partners. What you want to do is restrict the list. In order to do that add a drop down list in the view and fill it with some possible partners. This can be done either by putting a list in ViewData or expanding the model passed back to the view. Both have advantages. Now when you change the drop down reload the page but append a parameter which is the filter. In the controller check for that parameter in the action, if it isn't present then return an unfiltered list, if it is then apply a filter and return the list. The view will just dumbly display whatever you give it.

As for the filtering you might want to try using LINQ.

like image 42
stimms Avatar answered Oct 13 '22 10:10

stimms