JQgrid不适用于MVC4和EntityFramework

时间:2014-02-14 21:35:03

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

嗨,我开始研究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);
            }
        }
    }
 }

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

提前致谢

2 个答案:

答案 0 :(得分:2)

请尝试如下。

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

           //your code here            

            return Json(jsonData,JsonRequestBehavior.AllowGet);
        }

答案 1 :(得分:0)

将此网址:'/ Home / DynamicGridData'更改为此网址:'@ Url.Action(“DynamicGridData”)'