如何向mvc3中的控制器发送值列表

时间:2021-07-08 22:15:17

I Have a jq grid where the user will be selecting a list of check boxes i want to send them to controller

我有一个jq网格,用户将在其中选择我要发送给控制器的复选框的列表。

I am getting the selected views in a JavaScript function

我正在一个JavaScript函数中获取所选的视图

I have tried in the following ways

我试过以下方法

  1. Created a variable in the script and tried to pass it to action not worked
  2. 在脚本中创建一个变量,并尝试将其传递给无效的操作
  3. tried with the view bag not worked
  4. 试用的视图包不起作用
  5. tried with adding a property in model add adding values to list from function not worked
  6. 尝试在模型中添加属性,从未使用的函数中添加值到列表中

here is my script in view

这是我的脚本

 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);


}

and here is action in the controller

这是控制器中的动作

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);
    }

the view is in jqgrid and view is as follows:

视图在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}
    ];

and custom formater for adding values for checkboxes:

以及用于为复选框添加值的自定义格式器:

  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;
};

1 个解决方案

#1


1  

OK. First of all, remove the standard checkFormatter and add the following custom formatter for that column:

好的。首先,删除标准的checkFormatter,并为该列添加以下自定义格式化程序:

function approve(cellvalue, options, rowobject) {        
    return '<input type="checkbox" id="chk' + cellvalue + '" value="false" onclick="chkChange(\'#chk' + cellvalue + '\')" />';
}

Second, add the following function to handle the check boxes changes:

其次,添加以下函数处理复选框的更改:

function chkChange(id) {        
    if ($(id).val() != 'false')
        $(id).val('false');
    else
        $(id).val('true');        
}  

Now, enter this function to get rows with selected checkboxes and send them to controller:

现在,输入此函数获取选中复选框的行并将其发送给控制器:

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 ...
        });
}

Finally, change your action method like this to get working:

最后,改变你的行动方法,比如:

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 ...
}

Any questions?! I'm here! if no, near...!

有什么问题吗? !我在这里!如果没有,附近…!

#1


1  

OK. First of all, remove the standard checkFormatter and add the following custom formatter for that column:

好的。首先,删除标准的checkFormatter,并为该列添加以下自定义格式化程序:

function approve(cellvalue, options, rowobject) {        
    return '<input type="checkbox" id="chk' + cellvalue + '" value="false" onclick="chkChange(\'#chk' + cellvalue + '\')" />';
}

Second, add the following function to handle the check boxes changes:

其次,添加以下函数处理复选框的更改:

function chkChange(id) {        
    if ($(id).val() != 'false')
        $(id).val('false');
    else
        $(id).val('true');        
}  

Now, enter this function to get rows with selected checkboxes and send them to controller:

现在,输入此函数获取选中复选框的行并将其发送给控制器:

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 ...
        });
}

Finally, change your action method like this to get working:

最后,改变你的行动方法,比如:

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 ...
}

Any questions?! I'm here! if no, near...!

有什么问题吗? !我在这里!如果没有,附近…!