2

ユーザーがチェックボックスのリストを選択するjqグリッドがあり、コントローラーに送信したい

JavaScript 関数で選択したビューを取得しています

私は次の方法で試しました

  1. スクリプトで変数を作成し、それをアクションに渡そうとしましたが機能しませんでした
  2. ビューバッグで試してみましたが機能しませんでした
  3. モデルにプロパティを追加してみました 関数からリストに値を追加する 機能しませんでした

ここに私のスクリプトが表示されています

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

1 に答える 1

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

質問は?!私はここにいます!いいえ、近く...!

于 2013-08-11T17:37:51.197 に答える