Click here to Skip to main content
16,019,876 members
Please Sign up or sign in to vote.
1.50/5 (2 votes)
I have looked up a lot of posts regarding this but still cant figure out how to send the data source to controller (instead of applied filters to the grid). Instead of querying the repository again in the back end I would like to receive the jQGrid data source in the MVC Controller.

I need to somehow get the datasource as a collection in the controller so I can export it to CSV file.

I have defined the following button in jqgrid and next block of code are the properties of the jQgrid I'm setting. I don't know how can I pass the jQgrid's current datasource(filters applied) to the MVC controller.

JavaScript
$("#btnExportCsv").click(function () {
            $.blockUI({ css: {
                border: 'none',
                padding: '15px',
                backgroundColor: '#000',
                '-webkit-border-radius': '10px',
                '-moz-border-radius': '10px',
                color: '#fff'
            }, message: '<h1>Exporting to CSV</h1><p><img src="/cms/themes/cm-blue/img/busy.gif" /></p>'
            });

            generating = true;

            $.ajax({
                type: 'POST',
                dataType: 'json',
                url: exportCsvUrl,
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    $.jGrowl(XMLHttpRequest.responseText, { header: 'Error exporting csv' });
                    generating = false;
                },
                complete: function () {
                    $.unblockUI();
                },
                success: function () {
                    growl("Export completed successfully", "Export to Csv");
                }
            });

        });



-----------------------------------

JavaScript
jQGrid params:

$("#myGrid").jqGrid({
        colNames: [
    .
    .
    .
        ],
        colModel: [...]
        mtype: 'POST',
        datatype: 'json',
        pager: '#crPager',
        page: defaultGridParams.Page,
        sortname: defaultGridParams.SortName,
        sortorder: defaultGridParams.SortOrder,
        rowNum: 10,
        rowList: [10, 20, 50, 100],
        url: Controllers/control,
        height: 'auto',
        loadui: 'block',
        width: 1200,
        caption: sGridCaption,
        hidegrid: false,
        viewrecords: true,
        ShrinkToFit: false,

.
.
.
Posted
Updated 30-Jan-12 5:04am
v3
Comments
Sergey Alexandrovich Kryukov 27-Jan-12 19:02pm    
And where is your question? What's the problem?
--SA
Mastersev 27-Jan-12 19:12pm    
I donno how to send the data source to the controller

1 solution

You want to pass the grid data to controller action right?
Follow the steps below:
1)First collect the data in the format of object collection
ex:
C#
var mydata = [{Name:"X",Address:"X1"},{Name:"Y",Address:"Y1"}]

Assume that Name & Address are columns in grid.
2)Add following attributes to $.ajax()
C#
data: JSON.stringify(mydata),
contentType:'application/json;charset=utf-8',

3)Create a model class to hold above data
C#
class Employee{ public string Name{get;set;} public string Address{get;set;}}

4)define controller action
C#
[HttpPost] 
ActionResult exportCsvUrl(List<Employee> employees)
{
...
}
 
Share this answer
 
v4
Comments
Anuja Pawar Indore 2-Feb-12 2:10am    
Added pre tag
brijesh vaidya 20-Mar-13 3:44am    
thank you very much, this will solve my big prob.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900