[英]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.