Jquery 如何向mvc3中的控制器发送值列表
我有一个jq网格,用户将在其中选择我想发送给控制器的复选框列表 我在JavaScript函数中获取所选视图 我试过以下几种方法Jquery 如何向mvc3中的控制器发送值列表,jquery,asp.net-mvc,asp.net-mvc-4,razor,jqgrid,Jquery,Asp.net Mvc,Asp.net Mvc 4,Razor,Jqgrid,我有一个jq网格,用户将在其中选择我想发送给控制器的复选框列表 我在JavaScript函数中获取所选视图 我试过以下几种方法 在脚本中创建了一个变量,并尝试将其传递给操作“未工作” 尝试使用视图包时未工作 尝试在模型中添加属性“添加”“向列表中添加值”“函数未工作” 这是我的脚本 function ChangeStatusSource() { //checking if any records are selected or not var type= StatusRecor
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);
}
public ActionResult文件上传(我需要获取列表值)
{
List sourceId=ViewBag.Id;
LoggingManager.Enter();
var res=new SourceFieldModel{FieldID=null};
LoggingManager.Exit();
返回视图(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}
];
$(文档).ready(函数(){
$(“a.button”).button();
var url='@url.Action(“ListAll”)'+'?s_partId='+'@ViewBag.SourceId';
var colNames=['',
“编辑”,
“忽略”,
“字段/角色”,
“条款”,
'字段类型'];
var colModel=[
{name:'Id',index:'Id',align:'center',格式化程序:checkFormatter,宽度:20},
{name:'Edit',index:'Id',align:'center',格式化程序:myCustomSourceFormatter,宽度:60},
{name:'Ignore',index:'Ignore',align:'center',格式化程序:dcheckFormatter,宽度: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;
};
var myCustomSourceFormatter=函数(cellvalue、options、rowObject){
$(“cellvalue”).val(cellvalue);
var数据=1;
var returnValue=“”;
返回值;
};
好。首先,删除标准的检查格式化程序,并为该列添加以下自定义格式化程序:
function approve(cellvalue, options, rowobject) {
return '<input type="checkbox" id="chk' + cellvalue + '" value="false" onclick="chkChange(\'#chk' + cellvalue + '\')" />';
}
现在,输入此函数以获取带有选中复选框的行并将其发送到控制器:
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 ...
});
}
函数提交(){
//获取网格的所有行:
var grid=$(“#gridID”);
jqGrid('resetSelection');
var myData=grid.jqGrid('getRowData');
//获取已选中复选框的行
//并将其Id值放入数组中:
var-id=[];
对于(var i=0;i
最后,更改您的操作方法,如下所示:
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 ...
}
public ActionResult文件上载(字符串ID)
{
//将json数组字符串反序列化为列表:
IList sourceIds=new
JavaScriptSerializer()。反序列化(Ids);
//现在做你需要的任何事。。。
}
有问题吗?!我在这里!如果没有,靠近…!显示网格中复选框列的格式设置程序…修改后的问题请执行检查我想在不使用ajax的情况下发送,因为我打开了一个来自这些值的弹出窗口,我在弹出窗口中使用文件上载,我无法使用ajax发送values@user2189168-通过ajax或其他方式发送没关系。您只需创建一个像ids
这样的数组,使用JSON.stringify()
将其字符串化,并通过一个查询字符串发送…var id='';var id=[];$。每个($($)输入[id^='chkOF\'])函数(index,ctrl){if($(ctrl).is(':checked'){id+=$(ctrl).val()+',';ids.push(id);}});OpenPopup('@Url.Action(“FileUpload”,new{这是我需要传递的hwer}'),gridReloadSourceField);您能建议如何传递吗here@user2189168-我认为你不能使用@Url.Action()
这里。因为它是在服务器上处理的,而javascript是在客户端处理的
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 ...
}