【问题标题】:JQgrid is not working in MVC4 and EntityFrameworkJQgrid 在 MVC4 和 EntityFramework 中不起作用
【发布时间】:2014-02-14 21:35:03
【问题描述】:

您好,我开始在 JQGrid 上工作,我关注了从互联网博客Jqgrid with MVC 获得的帖子 我的代码如下所示:

    @{
        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);
            }
        }
    }
 }

问题是,索引页面中没有显示任何内容。我厌倦了尝试,请帮助我。如果你发现我的代码有任何缺陷。

提前致谢

【问题讨论】:

  • 您是否尝试过调试请求以查看它是否返回了正确的 JsonData?
  • 是的,我做到了。它返回数据。但我认为它没有赶上 JQgrid。
  • 检查您的退货行数据,这可能是旧版本的格式。您的单元格对象应该是行需要的内容

标签: asp.net asp.net-mvc entity-framework jqgrid-asp.net


【解决方案1】:

请尝试如下。

[HttpPost]
public JsonResult DynamicGridData(string sidx, string sord, int page, int rows)
        {

           //your code here            

            return Json(jsonData,JsonRequestBehavior.AllowGet);
        }

【讨论】:

  • 嗨 Sampath,我需要为此控制器操作添加新视图吗?我是否遗漏了任何参考资料?
  • Nope.Just 修改您现有的 'DynamicGridData' Action 方法的返回类型如上(即返回 Json(jsonData,JsonRequestBehavior.AllowGet); )并将属性设置为 [HttpPost] 并尝试。
  • 嗨 Sampath,我按照您的建议尝试了,结果相同,没有任何返回。只是一个空白页。如果您愿意,我可以将我的示例项目发送给您,如果您输入您的邮件 ID。
  • 嗨 Sampath,没有什么对我有用,我添加了新的空控制器并按照您的建议使用 [HttpPost] 填充相同的数据,并添加了与我的旧视图相同的新视图。现在它给我带来了新的错误 404,我也检查了我的路由配置。据我所知,它是正确的。但我不知道它现在完全错了。
  • @NandishHosmane 你能把你的项目发给我,因此它是一个样本吗?您可以在 SOF 个人资料中找到我的电子邮件。
【解决方案2】:

将此网址:'/Home/DynamicGridData' 更改为以下网址:'@Url.Action("DynamicGridData")'

【讨论】:

  • 对不起,但这些似乎是相同的路径。你能强调一下区别吗?谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-03-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多