Asp.net JQgrid不在MVC4和EntityFramework中工作
大家好,我开始在JQGrid上工作,我关注了一篇来自互联网博客的帖子 我的代码如下所示:Asp.net JQgrid不在MVC4和EntityFramework中工作,asp.net,asp.net-mvc,entity-framework,jqgrid-asp.net,Asp.net,Asp.net Mvc,Entity Framework,Jqgrid Asp.net,大家好,我开始在JQGrid上工作,我关注了一篇来自互联网博客的帖子 我的代码如下所示: @{ ViewBag.Title = "Home Page"; } <h2>@ViewBag.Message</h2> <ol class="round"> <script type="text/javascript"> jQuery(document).ready(function () {
@{
ViewBag.Title = "Home Page";
}
<h2>@ViewBag.Message</h2>
<ol class="round">
<script type="text/javascript">
jQuery(document).ready(function () {
jQuery("#list").jqGrid({
url: '/Home/DynamicGridData',
datatype: 'json',
mtype: 'POST',
colNames: ['UserId', 'FirstName', 'LastName', 'CreatedBy', 'Designation', 'City'],
colModel: [
{ name: 'UserId', index: 'UserId', width: 40, align: 'left' },
{ name: 'FirstName', index: 'FirstName', width: 40, align: 'left' },
{ name: 'LastName', index: 'LastName', width: 400, align: 'left' },
{ name: 'CreatedBy', index: 'CreatedBy', width: 400, align: 'left' },
{ name: 'Designation', index: 'Designation', width: 400, align: 'left' },
{ name: 'City', index: 'City', width: 400, aligh: 'left' }],
pager: jQuery('#pager'),
rowNum: 2,
rowList: [5, 10, 20, 50],
sortname: 'Id',
sortorder: "desc",
viewrecords: true,
imgpath: '/content/images',
caption: 'My first grid'
});
});
</script>
<%-- HTML Required--%>
<h2>My Grid Data</h2>
<table id="list" class="scroll" cellpadding="0" cellspacing="0"></table>
<div id="pager" class="scroll" style="text-align:center;"></div>
</ol>
using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Web;
using System.Web.Mvc;
using System.Linq.Expressions;
using UserInfoGrid.Models;
using System.Linq;
using System.Linq.Dynamic;
namespace UserInfoGrid.Controllers
{
public class HomeController : Controller
{
UserInfoEntities db = new UserInfoEntities();
public ActionResult Index()
{
ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";
return View();
}
public ActionResult About()
{
ViewBag.Message = "Your app description page.";
return View();
}
public ActionResult Contact()
{
ViewBag.Message = "Your contact page.";
return View();
}
public JsonResult DynamicGridData(string sidx, string sord, int page, int rows)
{
int pageIndex = Convert.ToInt32(page) - 1;
int pageSize = rows;
int totalRecords = db.Users.Count();
int totalPages = (int)Math.Ceiling((float)totalRecords / (float)pageSize);
// var userInfo = db.User(sidx + " " + sord).Skip(pageIndex * pageSize).Take(pageSize);
//var userInfo = db.Users.OrderBy((sidx+""+sord).Skip(pageIndex * pageSize).Take(pageSize)).ToList();
var userInfo = db.Users.OrderBy(sidx + " " + sord).Skip(pageIndex * pageSize).Take(pageSize);
var jsonData = new
{
total = totalPages,
page,
records = totalRecords,
rows = (
from u in userInfo
select new
{
i = u.UserId,
cell = new string[] { u.UserId.ToString(), u.FirstName, u.LastName, u.CreatedBy.ToString(), u.Designation, u.City.ToString() }
//cell = new string[] { "", "", "", "" }
}).ToArray()
};
return Json(jsonData);
}
}
}
}
问题是,索引页中没有显示任何内容。我厌倦了尝试,请帮帮我。如果你发现我的代码有任何缺陷
提前感谢请尝试以下内容
[HttpPost]
public JsonResult DynamicGridData(string sidx, string sord, int page, int rows)
{
//your code here
return Json(jsonData,JsonRequestBehavior.AllowGet);
}
将此url:“/Home/dynamicgridata”更改为此url:“@url.Action(“dynamicgridata”)”是否尝试调试请求以查看它是否返回正确的JsonData?是的。它返回数据。但我不认为它捕获JQgrid。检查您的返回行数据,这可能是旧版本的格式。您的cells对象应该是行需要显示的内容。我是否需要为此控制器操作添加新视图。我是否丢失了视图中的任何引用?没有。只需修改您现有的“DynamicGridData”操作方法的返回类型如上所述(即返回Json(jsonData,JsonRequestBehavior.AllowGet);),并将属性设置为[HttpPost],然后重试。嗨,Sampath,我按照您的建议进行了尝试,结果相同,没有返回任何内容。只是一张空白页。如果您愿意,我可以将我的示例项目发送给您,如果您输入您的邮件id。嗨,Sampath,对我来说没有任何效果,我添加了新的空控制器,并按照您的建议使用[HttpPost]填充了相同的数据,并添加了与旧视图相同的新视图。现在它给我带来了新的错误404,我也检查了我的路由配置。就我所知,这是正确的。但我不知道它现在完全错了。@NandishHosmane你能帮我把你的项目寄过来吗,因此它是一个样本项目?您可以在SOF配置文件中找到我的电子邮件。对不起,这些路径似乎是相同的。你能强调一下区别吗?谢谢