在我的asp.net mvc中,使用jqGrid绑定数据。代码如下。但它没有加载数据,也没有在调试时遇到控制器动作。以下代码中的问题是什么?警报显示网址但未点击控制器操作。帮助将是欣赏。如果您需要更多信息,请告诉我
@model Stud.Areas.Admin.Models.AdminVM
@using Stud.Common.Extension;
<h2>MD Index</h2>
<br />
<div style="margin-top: -45px">
<div class="col-md-12">
<div id="gridDivmsg" style="clear: both;">
<table id="jqGrid" ></table>
<div id="jqGridPagermsg"></div>
</div>
</div>
</div>
@section scripts{
<link href="~/Content/themes/base/jquery-ui.css" rel="stylesheet" />
<link href="~/Content/jquery.jqGrid/ui.jqgrid.css" rel="stylesheet" />
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script src="~/Scripts/jquery-ui-1.12.1.min.js"></script>
<script src="~/Scripts/i18n/grid.locale-en.js"></script>
<script src="~/Scripts/jquery.jqGrid.min.js"></script>
@*<script src="~/Scripts/MDScript.js"></script>*@
@Html.jqGridSetup()
<script>
$(document).ready(function () {
var $grid = $('#jqGrid');
var $gridDiv = $('#gridDivmsg');
function reloadGridmsg() {
var urlName = '@Url.Action("GetMessagesForGrid", "MD")';
$grid.jqGrid("setGridParam", { url: urlName, datatype: "json", page: 1 }).trigger("reloadGridmsg");
$gridDiv.show();
alert(urlName);
}
$grid.jqGrid({
editurl: '@Url.Action("EditRowMessage")',
datatype: 'local',
styleUI: 'Bootstrap',
colNames: ['Id', 'Message Key', 'Message Value', 'Message Status'],
colModel: [
{ name: 'Id', index: 'Id', width: 1, hidden: true, editable: true, edittype: 'text', editrules: { required: true } },
{ name: 'MessageKey', index: 'MessageKey', width: 1, editable: true, edittype: 'text', editrules: { required: true } },
{ name: 'MessageValue', index: 'MessageValue', width: 1, editable: true, edittype: 'textarea', editrules: { required: true } },
{ name: 'MessageStatus', index: 'MessageStatus', width: 1, editable: true, edittype: 'select', editoptions: { value: { 'Active': 'Active', 'InActive': 'InActive' }, defaultValue: 'Active' }, editrules: { required: true } }
],
responsive: true,
loadonce: true,
pager: $('#jqGridPagermsg'),
height: 'auto',
sortname: 'Id',
rowNum: 20,
autowidth: true,
viewrecords: true,
altRows: true,
altclass: 'jqGridAltRow'
});
$grid.jqGrid('filterToolbar', { autosearch: true, searchOnEnter: false, defaultSearch: "cn" });
$grid.jqGrid('navGrid', "#jqGridPagermsg", {
edit: true,
add: true,
del: false,
search: false,
refresh: false,
view: false,
position: "left",
cloneToTop: false
},
{
editCaption: "Edit User",
recreateForm: true,
checkOnUpdate: true,
checkOnSubmit: true,
closeAfterEdit: true,
errorTextFormat: function (data) {
return 'Error: ' + data.responseText
},
afterComplete: function () {
reloadGridmsg();
}
},
{
addCaption: "Add User",
closeAfterAdd: true,
recreateForm: true,
errorTextFormat: function (data) {
return 'Error: ' + data.responseText
},
afterComplete: function () {
reloadGridmsg();
}
},
{
errorTextFormat: function (data) {
return 'Error: ' + data.responseText
}
});
reloadGridmsg();
});
</script>
}
Controller Action方法如下。
public ActionResult GetMessagesForGrid(StudVM model)
{
if (!IsAuthorized(Enums.Rights.Admin))
return View("NoAccess");
var gridData = _activityService.GetVCRMessagesList();
int totalRecords = gridData.Count();
int totalPages = (int)Math.Ceiling((float)totalRecords / (float)model.rows);
var jsonData = new
{
total = totalPages,
page = model.page,
records = totalRecords,
rows = gridData.Select(d => new { Id = d.Id, cell = new object[] { d.Id, d.MessageKey, d.MessageValue, d.MessageStatus } }).ToArray()
};
return Json(jsonData, JsonRequestBehavior.AllowGet);
}
直到我得到如下空格:
答案 0 :(得分:0)
您的控制器的参数model
类型为StudVM
,但未在您的javascript网址中传递。
尝试传递StudVM
类型的参数。它可以解决您的问题。
$grid.jqGrid("setGridParam", { url: urlName,postData: { model:<your model>}, datatype: "json", page: 1 }).trigger("reloadGridmsg");
答案 1 :(得分:0)
您需要让action方法遵循以下方法签名
'POUJON702'
我也没有看到你没有将网格数据类型设置为json并在定义网格时指定url而不是在网格客户端然后重新加载它的原因
USE Sandbox;
GO
CREATE TABLE #Sample ([KEY] varchar(15),
WORKSTATION varchar(5),
[STATUS] varchar(6),
USERNAME varchar(10),
[TIMESTAMP] datetime);
INSERT INTO #Sample
VALUES ('181861-0001-001',NULL,'Closed',NULL,'2015-07-01T18:19:48.527'),
('181861-0001-001',NULL,'Closed',NULL,'2015-07-01T20:20:46.383'),
('181861-0001-001','1AHVW','Open','ANDJOH0427','2015-07-01T13:18:46.547'),
('181861-0001-001','1AHVW','Closed','ANDJOH0427','2015-07-01T14:11:41.920'),
('181861-0001-001','1ALVW','Closed','DWYGRE0609','2015-07-01T18:29:39.127'),
('181861-0001-001','1ALVW','Closed','DWYGRE0609','2015-07-01T18:29:40.300'),
('181861-0001-001','1AHVW','Closed','HORDOU0521','2015-07-01T19:27:34.667'),
('181861-0001-001','1AHVW','Closed','HORDOU0521','2015-07-01T19:44:36.167'),
('181861-0001-001','1AQCI','Open','POUJON702','2015-07-02T00:46:37.540'),
('181861-0001-001','1ALVW','Open','PRIADA747','2015-07-01T14:51:02.937'),
('181861-0001-001','1ALVW','Open','PRIADA747','2015-07-01T15:29:48.357'),
('181861-0001-001','1ALVW','Open','PRIADA747','2015-07-01T16:13:20.953'),
('181861-0001-001','1ALVW','Open','PRIADA747','2015-07-01T17:49:42.717'),
('181861-0001-001','1ALVW','Closed','PRIADA747','2015-07-01T17:53:28.217'),
('181861-0001-001','1ALVW','Open','PRIADA747','2015-07-01T18:34:11.043'),
('181861-0001-001','1ALVW','Closed','PRIADA747','2015-07-01T19:20:11.540');
GO
SELECT *
FROM #Sample;
GO
WITH Starts AS(
SELECT [KEY],
WORKSTATION,
USERNAME,
[TIMESTAMP],
NULLIF(MIN(ISNULL(CASE STATUS WHEN 'Open' THEN [TIMESTAMP] END,'20550101')) OVER (PARTITION BY [KEY], WORKSTATION, USERNAME),'20550101') AS StartTime
FROM #Sample S)
SELECT [KEY],
WORKSTATION,
USERNAME,
StartTime,
MAX([TIMESTAMP]) AS EndTime,
DATEDIFF(SECOND, StartTime, MAX([TIMESTAMP])) AS Seconds
FROM Starts
GROUP BY [KEY],
WORKSTATION,
USERNAME,
StartTime;
GO
DROP TABLE #Sample;
如果您需要知道如何使该操作方法自定义查看 This solution