如何使用JQGrid从Controller解析数据以进行查看



我正在尝试将数据从控制器传递到GQ网格。我已经在另一个文件中实现了SQL操作——选择一行,并将列表类型为List<Models.ViewModel.Itegration>的对象返回给Controller。我实现了JsonResult类型的控制器,它以json格式返回数据。控制器使用[HttpGet]Attritute。我已经附上了我的控制器代码,html,js文件和问题的截图。Google控制台没有显示任何问题。表正在准备就绪,但表数据正在显示。我想";传递数据";正确地如果有人能检查我的代码,让我知道我错在哪里,那对我来说会很有帮助。

此外,有没有任何软件,我可以在连接到服务器时检查问题的来源,因为在这种情况下,谷歌检查工具根本没有帮助。

在我的控制器中,通过使用断点,我检查了Integrations value = db.GetIntegrationRow();中的value中的值是否正确。控制器:

#region Namespace
using MCNIDev.DBAccess;
using MCNIDev.Models.ViewModel;
using MCNIDev.Processor.Services;
using System.Web.Mvc;
#endregion
namespace MCNIDev.Content
{
/// <summary>
/// This controller class is responsible for all action in  Integration view
/// </summary>
public class IntegrationsController : Controller
{
public ActionResult Details()
{
return PartialView();
}
/// <summary>
/// To get the data for JQGrid
/// </summary>
[HttpGet]
public JsonResult Detail()
{
IntegrationsProcessor db = new IntegrationsProcessor(new MCNIDbContext());
Integrations value = db.GetIntegrationRow();
return new JsonResult()
{
Data = value,
JsonRequestBehavior = JsonRequestBehavior.AllowGet
};

}
}
}

HTML文件

@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>
@*Bundle file includes the path to single JavaScript file Integration that does GQGrid operation *@
@Scripts.Render("~/bundles/integrations")
<script>
$(document).ready(function(){
Integrate.GetValue();
});
</script>

JavaScript文件:

var Integrate = function() {
function GetValue() {
$("#tblSelectIntegrations").jqGrid({
mtype: "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: "",
page: "page",
total: "total",
records: "records",
repeatitems: false
},
autowidth: true,
multiselect: false,
loadonce: false,
ajaxGridOptions: { cache: false },

}).navGrid("tblSelectIntegrations", { edit: false, add: false, del: false, search: false, refresh: false });
}
return {
GetValue: GetValue
};
}();

问题在于控制器。我试图在不告诉JQGrid在哪里插入数据的情况下发送数据。

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

将以下代码替换为以上代码

return new JsonResult()
{
Data = value,
JsonRequestBehavior = JsonRequestBehavior.AllowGet
};

在我的第一段代码中,我提到了将数据添加到行中。

return new JsonResult()
{
Data = value,
JsonRequestBehavior = JsonRequestBehavior.AllowGet
};

用以下代码替换上述代码。

int totalRecords = value.Count();
var totalPages = (int)Math.Ceiling((float)totalRecords / (float)rows);
var JsonData = new
{
total = totalPages,
page,
records = totalRecords,
rows = value 
};
return Json(JsonData, JsonRequestBehavior.AllowGet);

最新更新