当我在asp.net mvc4中从一个页面重定向到另一个页面时,JQgrid没有加载数据

时间:2014-03-07 06:27:16

标签: asp.net-mvc-4 razor jqgrid

我的asp.net mvc4中的jqgrid存在问题。我正在创建用户表单,在此我将向用户详细信息添加数据,当我单击提交时,它将被添加到数据库中,并根据其结果将其重定向到相应的页面。 如果添加成功,那么它将被重定向到用户列表页面,我有我的JQgrid,问题是,我可以看到网格,但网格中没有数据。什么是我的问题。

我添加新用户的控制器代码如下所示:

    public ActionResult CreateNewUser(Tbl_Users tbl_users)
    {
        int userId = 1;
        tbl_users.CreatedBy = userId;
        tbl_users.CreatedOn = DateTime.Now;
        tbl_users.ModifiedBy = null;
        tbl_users.ModifiedOn = null;
        if (ModelState.IsValid)
        {

            db.Tbl_Users.Add(tbl_users);
            db.SaveChanges();
            return RedirectToAction("**ListUsers**", "**Admin**");
        }

        ViewBag.UserCity = new SelectList(db.Tbl_Mst_City, "CityId", "CityName", tbl_users.UserCity);
        ViewBag.UserDesignation = new SelectList(db.Tbl_Mst_Designation, "DesignationID", "Designation", tbl_users.UserDesignation);
        ViewBag.RoleId = new SelectList(db.Tbl_Roles, "RoleID", "Role", tbl_users.RoleId);
        ViewBag.CreatedBy = new SelectList(db.Tbl_Users, "UserID", "UserName", tbl_users.CreatedBy);
        return View(tbl_users);
    }

我的控制器动作加载我的网格是

    public ActionResult ListUsers()
    {
        return View();
    }

    public JsonResult GetUSerDetails(string sidx = "UserID", string sord = "asc", int page = 1, int rows = 5)
    {
        int pageIndex = Convert.ToInt32(page) - 1;
        int pageSize = rows;
        int totalRecords = db.Tbl_Users.Count();
        int totalPages = (int)Math.Ceiling((float)totalRecords / (float)pageSize);

        var userdata = db.Tbl_Users.OrderBy(sidx + " " + sord).Skip(pageIndex * pageSize).Take(pageSize)
            .Select(u =>
                new
                {
                    u.UserID,
                    u.UserName,
                    u.UserEmail,
                    u.UserMobile,
                    u.UserCity,
                    u.UserDesignation,
                    u.RoleId,
                    u.CreatedBy,
                    u.CreatedOn
                }).ToList();

        var jsonData = new
        {

            total = totalPages,
            page,
            records = totalRecords,

            rows = (
                from u in userdata.AsEnumerable()
                select new
                {
                    i = u.UserID,
                    cell = new string[] { u.UserID.ToString(), u.UserName, u.UserEmail, u.UserMobile, u.UserCity.ToString(), u.UserDesignation.ToString(), u.RoleId.ToString(), u.CreatedBy.ToString(), u.CreatedOn.ToString() }
                }).ToArray()
        };
        return Json(jsonData, JsonRequestBehavior.AllowGet);

    }

我的查看页面对于列表用户和JQgrid如下所示:

@model FSLIWeb.Models.Tbl_Tasks

@{
    ViewBag.Title = "ListUsers";
    Layout = "~/Views/Shared/AdminDashboardLayout.cshtml";
}


@Html.ActionLink(" User List", "Index")
<h2>Index</h2>
<table id="jQGridDemo" width:"1024px">
    </table>
    <div id="jQGridDemoPager" style="width:100%">
    </div>
    <script type="text/javascript">
        jQuery("#jQGridDemo").jqGrid({
            url: 'Admin/GetUserDetails',
            datatype: "json",
            colNames: ['UserID','UserName','UserEmail','Contact Num','City','UserDesignation','RoleId','CreatedBy','CreatedOn'],
            colModel: [
             { name: 'UserID', index: 'UserID', width: 75, align: 'center', sortable: true, editable: false, key: true, editrules: { required: true} },
             { name: 'UserName', index: 'UserName', width: 120, align: 'center', sortable: true, editable: true, edittype: 'text', editrules: { required: true} },
             { name: 'UserEmail', index: 'UserEmail', width: 100, align: 'center', sortable: true, editable: true, edittype: 'text', editrules: { required: true} },
             { name: 'UserMobile', index: 'UserMobile', width: 100, align: 'center', sortable: true, editable: true, edittype: 'text', editrules: { required: true} },
             { name: 'City', index: 'City', width: 100, align: 'center', sortable: true, editable: true, edittype: 'text', editrules: { required: true} },
             { name: 'UserDesignation', index: 'UserDesignation', width: 85, align: 'center', sortable: true, editable: true, edittype: 'text', editrules: { required: true} },
             { name: 'RoleId', index: 'RoleId', width: 70, align: 'center', sortable: true, editable: true, edittype: 'text', editrules: { required: true} },
             { name: 'CreatedBy', index: 'CreatedBy', width: 70, align: 'center', sortable: true, editable: true, edittype: 'text', editrules: { required: true} },
             { name: 'CreatedOn', index: 'CreatedOn', width: 70, align: 'center', sortable: true, editable: true, edittype: 'text', editrules: { required: true} }
             ],

            mtype: 'GET',
            loadonce: true,
            rowList: [5, 10, 20, 30],
            pager: '#jQGridDemoPager',
            sortname: 'UserID',
            viewrecords: true,
            sortorder: 'desc',
            width:"100%",
            caption: "List Of Users",
            //editurl: "//EditUser/"
        });

        jQuery("#jQGridDemo").jqGrid('navGrid', '#jQGridDemoPager',
        { edit: true, add: true, del: true, search: true },


           { url: "/Admin/EditUser/", closeAfterEdit: true, beforeShowForm: function (formid) { $("#UserID", formid).hide(); } },
           { url: "/Admin/AddNewUser/", closeAfterAdd: true, beforeShowForm: function (formid) { $("#UserID", formid).hide(); } },
           { url: "/Admin/DeleteUser/" }, {});

        $("#search").filterGrid("#grid", {
            gridModel: false,
            filterModel: [{
                label: 'Search',
                name: 'search',
                stype: 'text'
            }]
        });
    </script>

您宝贵的建议将帮助我解决这个问题。 在此先感谢

1 个答案:

答案 0 :(得分:0)

jqGrid定义中的数据URL应如下所示:

jQuery("#jQGridDemo").jqGrid({
    url: '/Admin/GetUserDetails',
    ...
});