asp.net-mvc ASP.NET MVC 过滤结果为列表/网格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1976981/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 00:00:02  来源:igfitidea点击:

ASP.NET MVC Filtering results in a list/grid

asp.net-mvc

提问by SlackerCoder

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.

出于某种原因,我坚持这一点。我需要根据同一视图中的 DropDownList 过滤视图中的结果。基本思想是这样的:我有一个属于各个合作伙伴的提供者列表,但提供者列表包含所有提供者(针对所有合作伙伴)。当有人只想查看合作伙伴时,我需要能够按合作伙伴显示提供者(否则,默认列表将是所有提供者)。我的观点目前是“默认”(显示全部),但出于某种原因,我坐在这里盯着显示器(过去 2 小时!)试图找出如何过滤这些结果。

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

任何建议从哪里开始/如何做?!

采纳答案by Josh Kodroff

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.

编辑:如果您想使用 jQuery 和 AJAX 执行此操作(这将提供更好的用户体验,因为只有细分列表会刷新),请参阅本教程

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

如果我理解正确的话,你基本上想要做一个 WebForms 风格的回发。

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.

在上面的代码中,如果表单提交是通过更改下拉列表中的值来触发的,则 btnSubmit 将为 null。因此,您要发布的操作可以判断用户是否打算完成她的更改。

回答by John M

To add upon the earlier answers.

补充之前的答案。

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

要创建下拉列表(在 ASP .NET MVC 3 中),我执行了以下操作:

Add code to Index.cshtml

将代码添加到 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()

在 Index() 的默认 ActionResult 中将代码添加到 YourModelNameController.cs

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);
    }

回答by Portman

There are manyways to skin this cat. Here's one.

很多方法可以给这只猫剥皮。这是一个。

Enclose your DropDownList in a form with METHOD=GET.

用 METHOD=GET 将您的 DropDownList 括在一个表单中。

<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.

然后,在您的控制器中,根据传入的 provider 的值进行过滤。记住将其视为 Nullable 参数,以便在它为空时可以有某种行为。

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

如果不发布您当前的一些代码,就很难获得比这更具体的内容。

回答by stimms

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.

让我们假设您可能正在将模型传递给视图,并且该模型是一个列表或 IEnummerable 合作伙伴。您想要做的是限制列表。为了做到这一点,在视图中添加一个下拉列表并用一些可能的合作伙伴填充它。这可以通过在 ViewData 中放置一个列表或扩展传递回视图的模型来完成。两者都有优势。现在,当您更改下拉菜单时,重新加载页面但附加一个参数,即过滤器。在控制器中检查动作中的该参数,如果它不存在则返回未过滤的列表,如果存在则应用过滤器并返回列表。该视图只会愚蠢地显示您提供的任何内容。

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

至于过滤,您可能想尝试使用 LINQ。

回答by Tomas Aschan

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.

您可能需要控制器操作的参数,可能是提供者的(可为空的?)id,以便在从数据库获取结果时过滤结果。然后只需使用相同的视图来列出它们,并在下拉列表更改时请求一个新列表。

回答by Developer

Best solution I know is that one.

我知道的最佳解决方案是那个。

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

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