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

时间:2014-10-21 12:03:22

标签: devexpress devexpress-mvc

我想获得一次在Devexpress MVC GridView上选择的行。没有点击每个项目。

1 个答案:

答案 0 :(得分:4)

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

  

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

     

示例代码段:

<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 parameter
GridView - How to get values of selected rows in the Controller's Action
How do I get my selected GridView rows into a Javascript variable?
DevExpress MVC GridView - How to get cell click event
How to highlight a particular row in a page of a DevExpress MVC GridView?