如何在 Devexpress MVC GridView 上获取选定的行?

Posted

技术标签:

【中文标题】如何在 Devexpress MVC GridView 上获取选定的行?【英文标题】:How can I get selected rows on Devexpress MVC GridView? 【发布时间】:2014-12-16 15:37:27 【问题描述】:

我想一次获取在 Devexpress MVC GridView 上选择的行。不是在每个点击的项目上。

【问题讨论】:

【参考方案1】:

参考:How to get all GridView selected keys and pass them to a Controller

您可以在客户端收集所有选定的值(例如,键) 通过ASPxClientGridView.GetSelectedFieldValues 方法和 将它们传递给:

通过 e.customArgs 字典 (Passing Values to Controller Action Through Callbacks) 的 GridView 回调操作; 任何控制器都通过隐藏的输入元素发布操作。

示例代码sn-p:

<script type="text/javascript">
    var selectedIDs;
    function OnBeginCallback(s, e) 
        //Pass all selected keys to GridView callback action
        e.customArgs["selectedIDs"] = selectedIDs;
    
    function OnSelectionChanged(s, e) 
        s.GetSelectedFieldValues("PersonID", GetSelectedFieldValuesCallback);
    
    function GetSelectedFieldValuesCallback(values) 
        //Capture all selected keys
        selectedIDs = "";
        for (var index = 0; index < values.length; index++) 
            selectedIDs += values[index] + ",";
        
        if (selectedIDs.length > 0)
            selectedIDs = selectedIDs.substring(0, selectedIDs.length - 1);
    
    function OnClick(s, e) 
        //Show all selected keys on client side
        alert(selectedIDs);
    
    function OnSubmitClick(s, e) 
        //Write all selected keys to hidden input. Pass them on post action.
        $("#selectedIDsHF").val(selectedIDs);
    
</script>

控制器

namespace Sample.Controllers 
    public class HomeController : Controller 
        PersonsList list = new PersonsList();

        [HttpGet]
        public ActionResult Index() 
            return View(list.GetPersons());
        

        [HttpPost]
        public ActionResult Index(string selectedIDsHF) 
            //Get all selected keys from hidden input
            string _selectedIDs = selectedIDsHF;

            return View(list.GetPersons());
        

        public ActionResult GridViewEditingPartial() 
            //Get all selected keys from e.customArgs on GridView callback
            string _selectedIDs = Request.Params["selectedIDs"];
            ViewData["_selectedIDs"] = _selectedIDs;

            return PartialView(list.GetPersons());
        

    

注意:客户端GetSelectedKeysOnPage Method返回当前页面中显示的选定行的键值。

参考:GridView - How to get selected rows as an action parameterGridView - How to get values of selected rows in the Controller's ActionHow do I get my selected GridView rows into a Javascript variable?DevExpress MVC GridView - How to get cell click eventHow to highlight a particular row in a page of a DevExpress MVC GridView?

【讨论】:

以上是关于如何在 Devexpress MVC GridView 上获取选定的行?的主要内容,如果未能解决你的问题,请参考以下文章

带有 mvc 和 Angular js 的 DevExpress 报告

如何从客户端禁用 DevExpress MVC 组合框(JS / JQuery)

gridview1 设置 内容居中

基于所选行的 Devexpress MVC Gridview 自定义排序

如何安装devexpress vcl 2014.1.2

仅在编辑单击时将 ReadOnly 属性设置为 devexpress 网格(MVC)的列