user2189168
user2189168

Reputation: 199

How to send list of Values to controller in mvc3

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

I am getting the selected views in a JavaScript function

I have tried in the following ways

  1. Created a variable in the script and tried to pass it to action not worked
  2. tried with the view bag not worked
  3. tried with adding a property in model add adding values to list from function not worked

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:

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

Upvotes: 2

Views: 2246

Answers (1)

Amin Saqi
Amin Saqi

Reputation: 18967

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

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

Upvotes: 1

Related Questions