我有一个jq网格,用户将在其中选择我要发送给控制器的复选框列表
我正在JavaScript函数中获取选定的视图
我已经尝试了以下方式
这是我的脚本
function ChangeStatusSource() {
//checking if any records are selected or not
var type= StatusRecords();
if (type=="@Resources.Strings.NoRecordsSelected") {
return;
}
//checking if any records are selected or not END
var id = '';
$.each($("input[id^='chkOF_']"), function (index, ctrl) {
if ($(ctrl).is(':checked')) {
id += $(ctrl).val() + ',';
}
});
OpenPopup('@Url.Action("FileUpload",new { i need to pass here})', gridReloadSourceField);
}
这是控制器中的动作
public ActionResult FileUpload( i need to get the list values)
{
List<string> sourceId = ViewBag.Id;
LoggingManager.Enter();
var res = new SourceFieldModel { FieldID = null};
LoggingManager.Exit();
return View(res);
}
该视图位于jqgrid中,视图如下:
$(document).ready(function () {
$("a.button").button();
var url = '@Url.Action("ListAll")' + '?s_partId=' + '@ViewBag.SourceId';
var colNames = ['<input type="checkbox" onclick="CheckAll(this.checked,\'chkOF_\',event);" name="checkall">',
'Edit',
'Ignore',
'Field/Role',
'Terms',
'Field Type'];
var colModel = [
{ name: 'Id', index: 'Id', align: 'center', formatter: checkFormatter, width: 20 },
{ name: 'Edit', index: 'Id', align: 'center', formatter: myCustomSourceFormatter, width: 60 },
{ name: 'Ignore', index: 'Ignore', align: 'center', formatter: dcheckFormatter, width: 100 },
{ name: 'FieldName', index: 'FieldName', align: 'left', width: 190 },
{ name: 'Term', index: 'Term', align: 'left' },
{name: 'FieldType',index:'FieldType', align:'left', width:200}
];
和自定义格式器,用于为复选框添加值:
var myCustomSourceFormatter = function (cellvalue, options, rowObject) {
$("cellvalue").val(cellvalue);
var data = 1;
var returnValue = "<input style='height:27px;' id='buttonedit' type='button' value='Edit' onclick=javascript:SourceFieldEdit('" + cellvalue + "') />";
return returnValue;
};
好的。首先,删除标准checkFormatter
并为该列添加以下自定义格式化程序:
function approve(cellvalue, options, rowobject) {
return '<input type="checkbox" id="chk' + cellvalue + '" value="false" onclick="chkChange(\'#chk' + cellvalue + '\')" />';
}
其次,添加以下功能来处理复选框更改:
function chkChange(id) {
if ($(id).val() != 'false')
$(id).val('false');
else
$(id).val('true');
}
现在,输入此函数以获取带有选中复选框的行,并将其发送到控制器:
function submit() {
// Getting all rows of the grid:
var grid = $("#gridID");
grid.jqGrid('resetSelection');
var myData = grid.jqGrid('getRowData');
// Getting those rows which has selected checkbox
// and put their Id values into an array:
var ids = [];
for (var i = 0; i < myData.length; i++) {
var sid = "#chk" + myData[i].Id;
if ($(sid).val() == 'true') {
var id = {
Id: myData[i].ID
};
ids.push(id);
}
}
// Send the array to the controller via ajax:
$.ajax({
url: "/Controller/Action?Ids=" + JSON.stringify(pics),
type: 'POST', dataType: 'json',
// other ajax options ...
});
}
最后,像这样更改操作方法即可开始工作:
public ActionResult FileUpload(string Ids)
{
// Deserialize the json array string to a List<string>:
IList<string> sourceIds = new
JavaScriptSerializer().Deserialize<IList<string>>(Ids);
// now do whatever you need ...
}
任何问题?!我在这!如果没有,附近...!
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句