Search code examples
asp.netasp.net-mvcjqgridjqgrid-asp.net

How to pass data from JsonResult to JQGrid in MVC?


I have used JQGrid to get the data from the controller which uses "GET". But, still I'm getting a blank table. I have also tried to apply breakpoints and check whether my variable "details" inside "Detail" Method getting value or not. It worked fine. I guess the problem is in returning the data in JSON format.

Controller

 [HttpGet]
        public JsonResult Detail()
        {

            IntegrationsProcessor db = new IntegrationsProcessor(new MCNIDbContext());
            DBModel.Integrations row = db.GetIntegrationRow();
            List<Integrations> details = new List<Integrations>
            {
                new Integrations
                {
                    IntegrationName = row.IntegrationName,
                    CompanyEmail = row.CompanyEmail
                }
            };


            return new JsonResult()
            {
                Data = details,
                JsonRequestBehavior = JsonRequestBehavior.AllowGet,
                MaxJsonLength = Int32.MaxValue
            };

HTML file

@using MCNIDev.CustomControls
@model MCNIDev.Models.ViewModel.Integrations


@{
    ViewBag.Title = "Details";
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=devicexb c-width" />
    <title>Integrations</title>
</head>
@Styles.Render("~/Content/toasterCss")
@Styles.Render("~/Content/dataPickerCss")
<style type="text/css">
    .btn {
        min-width: 80px !important;
    }
</style>
<body>
    @using (Html.BeginForm("", "Integrations", FormMethod.Post, new { enctype = "multipart/form-data", @class = "form-horizontal", id = "frmManifestOrders" }))
    {
        <div class="content-header">
            <div class="container-fluid">
                <div class="row mb-2">
                    <div class="col-sm-6">
                        <h1 class="m-0 text-dark">Integrations</h1>
                    </div><!-- /.col -->
                </div><!-- /.row -->
            </div><!-- /.container-fluid -->
        </div><!-- /.content-header -->

        <section class="content">
            <div class="container-fluid">
                <div class="card card-block">
                    <div class="row table-responsive">
                        <div class="col-md-12">
                            <div class="row">
                                <div class="col-md-12 col-sm-12">
                                    <table id="tblSelectIntegrations"></table>
                                    <div id="divSelect"></div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-md-12">
                            <div class="row">
                                <div class="col-md-12">
                                    <hr />
                                    <input type="button" class="btn btn-secondary" onclick="document.location.href='/Dashboard/Dashboard';" value="Cancel" />
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </section>
    }
</body>
</html>

@Scripts.Render("~/bundles/integrations")
<script>
$(document).ready(function(){
    Integrate.GetValue();
});
</script>

JQGrid File

var Integrate = function() {
function GetValue() {
    $("#tblSelectIntegrations").jqGrid({
        type: "GET",
        url: "/Integrations/Detail",
        datatype: "json",
        async: false,
        colNames: [
            "First Name", "Email Address"
        ],
        colModel: [
            //{
            //    name: '',
            //    key: false,
            //    width: 30,
            //    editable: true,
            //    formatter: function () {
            //        return "<input type='checkbox' value='Select' class='fm-button ui-state-default ui-corner-all fm-button-icon-left noPrint'\>";
            //    }
            //},
            { key: false, name: 'IntegrationName', index: 'IntegrationName', editable: false, width: 200 },
            { key: false, name: 'CompanyEmail', index: 'CompanyEmail', editable: false, width: 200 },
         //   { key: false, name: 'Blank', index: 'Blank', editable: false, width: 200 }
        ],
        pager: jQuery("#divSelect"),
        rowNum: 1,
        scroll: 0,
        height: $(window).innerHeight() - 450,
        width: "100%",
        viewrecords: true,
        caption: "Product",
        emptyrecords: "No records to display",
        jsonReader: {
            root: "rows",
            page: "page",
            total: "total",
            records: "records",
            repeatitems: false
        },
        autowidth: true,
        multiselect: false,
        loadonce: false,
        ajaxGridOptions: { cache: false },
        gridComplete: function () {
        },
       
    }).navGrid("tblSelectIntegrations", { edit: false, add: false, del: false, search: false, refresh: false });
}
return {
        GetValue: GetValue
    };
}();

Solution

  • The issue lies in controller. I am trying to send data without telling JQGrid where to insert data.

    var jsonData = new
                {
                    rows = value
                };
                return Json(jsonData, JsonRequestBehavior.AllowGet);
    

    Replace below code with above code

    return new JsonResult()
                {
                    Data = details,
                    JsonRequestBehavior = JsonRequestBehavior.AllowGet,
                    MaxJsonLength = Int32.MaxValue
                };
    

    In my first code I've mentioned that add my data in the rows.