Hello, I am using jQGrid in asp.net MVC project, but I can't connect to database using the below code
public JsonResult GetStudents(string sidx, string sort, int page, int rows)
{
using (studentEntities db = new studentEntities())
{
sort = (sort == null) ? "" : sort;
int pageIndex = Convert.ToInt32(page) - 1;
int pageSize = rows;
var StudentList = db.studentInfo.Select(
t => new
{
t.Id,
t.Name,
t.LastName,
t.@class
});
int totalRecords = StudentList.Count();
var totalPages = (int)Math.Ceiling((float)totalRecords / (float)rows);
if (sort.ToUpper() == "DESC")
{
StudentList = StudentList.OrderByDescending(t => t.Name);
StudentList = StudentList.Skip(pageIndex * pageSize).Take(pageSize);
}
else
{
StudentList = StudentList.OrderBy(t => t.Name);
StudentList = StudentList.Skip(pageIndex * pageSize).Take(pageSize);
}
var jsonData = new
{
total = totalPages,
page,
records = totalRecords,
rows = StudentList
};
return Json(jsonData, JsonRequestBehavior.AllowGet);
}
View(.cshtml code)
@{
ViewBag.Title = "Student Information";
}
<h2>Student Information</h2>
<div>
<table id="jqGrid"></table>
<div id="jqControls"></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-ui-1.10.0.js"></script>
<script src="~/Scripts/i18n/grid.locale-en.js"></script>
<script src="~/Scripts/jquery.jqGrid.min.js"></script>
<script src="~/Scripts/Script.js"></script>
}
and here is the jQgrid jquery code
$(function () {
$("#jqGrid").jqGrid({
url: "/Student/GetStudents",
datatype: 'json',
mtype: 'Get',
colNames: ['ID', 'Student Name', 'Last Name', 'Class'],
colModel: [
{ key: true, hidden: true, name: 'Id', index: 'Id', editable: true },
{ key: false, name: 'Name', index: 'Name', editable: true },
{ key: false, name: 'LastName', index: 'LastName', editable: true },
{key: false, name: '@class', index: '@class', edittable: true }
],
pager: jQuery('#jqControls'),
rowNum: 10,
rowList: [10, 20, 30, 40, 50],
height: '100%',
viewrecords: true,
caption: 'Students Records',
emptyrecords: 'No Students Records are Available to Display',
jsonReader: {
root: "rows",
page: "page",
total: "total",
records: "records",
repeatitems: false,
Id: "0"
},
autowidth: true,
multiselect: false
});
});
Model
namespace jtable.Models
{
using System;
using System.Collections.Generic;
public partial class studentInfo
{
public int Id { get; set; }
public string Name { get; set; }
public string LastName { get; set; }
public string @class { get; set; }
}
}
What can be the possible cause of the issue, while connecting to the database?
Your question explanation has complete code but no error details, can you provide what was the error while connecting to database ?? As I don't find any issue in the above code, please check if there is any data in the database table to which you have connected the project.
I have added a completely new article for explaining CRUD operations using jqGrid in MVC, well tested and with code details, you can read it here
It has all the details with script to create dummy database.
Subscribe to our weekly Newsletter & Keep getting latest article/questions in your inbox weekly