我有一个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;
};
答案 0 :(得分:1)
行。首先,删除标准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 ...
}
有问题吗?!我在这!如果没有,靠近......!