我想获得一次在Devexpress MVC GridView上选择的行。没有点击每个项目。
答案 0 :(得分:4)
参考: 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回调操作;
- 任何控制器通过隐藏的输入元素发布操作。
示例代码段:
<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?