ASP.NET MVC Filtering results in a list/grid

前端 未结 6 1818
我寻月下人不归
我寻月下人不归 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: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"))%>
        
    <%} %>
    

    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.

提交回复
热议问题