I'm not sure where I am doing wrong, I am unable to populate data into jqGrid control.
Tried different articles, and verified some video tutorials, but no luck.
Can anyone please help me to solve the issue!
Below is the code in HomeController
.
[HttpGet]
public ActionResult JQGridOrders()
{
return View();
}
[HttpGet]
public ActionResult GetOrders()
{
using (NorthwindEntities db = new NorthwindEntities())
{
var ordersList = db.Orders.Select(x => new OrderInfo {
OderID = x.OrderID,
CustomerID = x.CustomerID,
OrderDate = x.OrderDate,
Freight = x.Freight,
ShipName = x.ShipName,
ShipAddress = x.ShipAddress
}).ToList();
return Json(new { rows = ordersList }, JsonRequestBehavior.AllowGet);
}
}
My OrderInfo
class is below
public class OrderInfo
{
public int OderID { get; set; }
public string CustomerID { get; set; }
public DateTime? OrderDate { get; set; }
public decimal? Freight { get; set; }
public string ShipName { get; set; }
public string ShipAddress { get; set; }
}
Below is the View
@{ ViewBag.Title = "Showing Orders in JQGrid Control"; }
<h2>@ViewBag.Title</h2>
<link href="~/Content/Theme/jquery-ui.min.css" rel="stylesheet" />
<link href="~/Content/ui.jqgrid.css" rel="stylesheet" />
<script src="~/scripts/jquery-3.3.1.min.js"></script>
<script>
jQuery.browser = {};
(function () {
jQuery.browser.msie = false;
jQuery.browser.version = 0;
if (navigator.userAgent.match(/MSIE ([0-9]+)\./)) {
jQuery.browser.msie = true;
jQuery.browser.version = RegExp.$1;
}
})();
</script>
<script src="~/scripts/jqGrid/jquery.jqGrid.min.js"></script>
<script src="~/Scripts/bootstrap.min.js"></script>
<script src="~/scripts/jqGrid/grid.locale-en.js"></script>
<script>
$(function () {
$grid = $("#jqGrid").jqGrid({
url: '@Url.Action("GetOrders", "Home")',
mtype: 'GET',
dataType: 'json',
colModel: [
{ label: 'Order ID', name: 'OrderID' },
{ label: 'Customer ID', name: 'CustomerID' },
{ label: 'Order Date', name: 'OrderDate' },
{ label: 'Freight', name: 'Freight' },
{ label: 'Ship Name', name: 'ShipName' },
{ label: 'Ship Address', name: 'ShipAddress' }
],
loadonce: true
});
})
</script>
Try to change return type of action from ActionResult to JsonResult