ASP.NET MVC Filtering results in a list/grid

前端 未结 6 1808
我寻月下人不归
我寻月下人不归 2020-12-28 21:32

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 be

相关标签:
6条回答
  • 2020-12-28 22:23

    Best solution I know is that one.

    http://gridmvc.codeplex.com/SourceControl/latest

    0 讨论(0)
  • 2020-12-28 22:24

    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.

    0 讨论(0)
  • 2020-12-28 22:25

    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);
        }
    
    0 讨论(0)
  • 2020-12-28 22:28

    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.

    0 讨论(0)
  • 2020-12-28 22:29

    You probably want a parameter to your controller action, maybe a (nullable?) id of the provider, to filter the results already when you get them from DB. Then just use the same view to list them, and request a new list if the dropdownlist changes.

    0 讨论(0)
  • 2020-12-28 22:36

    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.

    0 讨论(0)
提交回复
热议问题