简体   繁体   English

在ASP.NET MVC中筛选列表视图的最佳方法

[英]Best way to filter a list-view in asp.net MVC

I have a list of data coming from the database and displaying in a table, that works exactly how I want. 我有一个来自数据库的数据列表,并显示在一个表中,该列表正是我想要的。

What I would like to do, is add a DropDownList to the page that "filters" the data in the table, based on the value of the selected item in the DropDonwList. 我想做的是,根据DropDonwList中所选项目的值,向页面中的“过滤”表数据中添加一个DropDownList。

For example, the DropDown has these values 例如,DropDown具有以下值

Assigned To Me
Assigned To Others

and the list of data, has an "assignedTo" field. 和数据列表具有“ assignedTo”字段。 When the value in the dropdown changes, I would like to update the list of data. 当下拉列表中的值更改时,我想更新数据列表。

In WebForms, I would use an UpdatePanel and a DropDownList with autoPostBack=True, how can I get the same effect in MVC? 在WebForms中,我将使用带有autoPostBack = True的UpdatePanel和DropDownList,如何在MVC中获得相同的效果?

You use JavaScript/jQuery to bind to onchange/onclick event, and there do a postback: 您使用JavaScript / jQuery绑定到onchange / onclick事件,然后执行回发:

$(function() {
   $("#myelement").click(function(){
      $("#secondelement").load('<%= Url.Action("Source") %>?selected=' + $(this).val());
   });
}

There're jQuery plugins that do similar things, for example http://dev.chayachronicles.com/jquery/cascade/index.html (not the best one, the first I found). 有些jQuery插件可以做类似的事情,例如http://dev.chayachronicles.com/jquery/cascade/index.html (不是最好的,我发现的第一个)。

You have a few options. 您有几种选择。 One way is to create a Controller method that manages the process of grabbing your data (say as a IList), and then uses the Json(...) method of the Controller to serialize it and send it back as JsonResult (here's an example: http://weblogs.asp.net/mehfuzh/archive/2009/04/28/using-of-json-result-in-asp-net-mvc-1-0.aspx ). 一种方法是创建一个Controller方法,该方法管理获取数据的过程(例如,作为IList),然后使用Controller的Json(...)方法对其进行序列化并作为JsonResult发送回去(这里是一个示例) : http : //weblogs.asp.net/mehfuzh/archive/2009/04/28/using-of-json-result-in-asp-net-mvc-1-0.aspx )。

On your front end, you can wire up some javascript on the dropdown list that uses jQuery to make a $.get ( http://api.jquery.com/jQuery.get/ ) passing back an id of sorts to determine your filter criteria. 在前端,您可以在下拉列表中连接一些JavaScript,该列表使用jQuery制作$ .get( http://api.jquery.com/jQuery.get/ ),并传回各种ID来确定您的过滤器标准。

Then you use the callback function of the $.get(...) call to manipulate your DOM as you see fit to visually depict the new list of data. 然后,您可以使用$ .get(...)调用的回调函数来按需操作DOM,以可视方式描绘新数据列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 筛选只能通过ASP.NET MVC中的javascript过滤的数据库记录列表的最佳方法是什么 - What is the best way to filter a list of database records that can only be filtered through javascript in ASP.NET MVC 使用ASP.NET MVC数据绑定View Model显示JQuery Dialog的最佳方法 - Best way to display a JQuery Dialog with ASP.NET MVC data bound View Model 在ASP.NET MVC中做ListView的最佳方法? - Best way to do ListView in ASP.NET MVC? 使用Asp.Net MVC在javascript中删除硬编码字符串的最佳方法 - Best way to remove hardcoded string in javascript with Asp.Net MVC 在ASP.NET MVC中处理日期的最佳方法-Javascript应用程序 - The best way to deal with dates in a ASP.NET MVC - Javascript application 如何在MVC asp.net中将LIST传递为JSON查看 - How to pass LIST to view as JSON in MVC asp.net ASP.Net MVC最佳实践,用于处理控制器事件,该事件不返回任何视图,但在完成时弹出一个窗口 - ASP.Net MVC best practice for handling controller event that returns no view but pops up a window on completion ASP.NET MVC3中特定于Razor视图的JQuery代码的最佳实践 - Best practices for Razor view-specific JQuery code in ASP.NET MVC3 asp.net mvc 2-使用View返回JavaScript - asp.net mvc 2 - return JavaScript with View 将JavaScript写入ASP.NET的最佳方法 - Best way to write javascript to ASP.NET
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM