【问题标题】:JQGrid ASP.Net MVC failed load data on many-to-many relation shipJQGrid ASP.Net MVC 在多对多关系上加载数据失败
【发布时间】:2012-02-20 09:06:53
【问题描述】:

我正在尝试使用 asp.net mvc3 Entity Framework 4(第一个模型)创建一个简单的用户角色管理项目,并使用 jqgrid 来显示数据。

但是为什么如果我将表用户和角色之间的数据关系更改为多对多关系, jqgrid 加载数据失败。

以下是我的项目数据:

项目数据:

  1. VS 2010
  2. ASP.Net MVC 3
  3. 视图:剃刀
  4. ORM:Linq 到实体
  5. JQGrid 版本:jqGrid 3.8

  6. 项目包:

    • EntityFramework" version="4.1.10715.0
    • jQuery" 版本="1.5.1
    • jQuery.UI.Combined" version="1.8.11
    • jQuery.Validation" 版本="1.8.0
    • jQuery.vsdoc" 版本="1.5.1"
    • Modernizr" version="1.7
    • MvcScaffolding" 版本="1.0.6
    • T4Scaffolding" 版本="1.0.5

型号:


- 用户

    public partial class User
{

    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)] 
    public int UserId { get; set; }
    public string UserName { get; set; }
    public string LoweredUserName { get; set; }
    public string MobileAlias { get; set; }
    public bool IsAnonymous { get; set; }
    public System.DateTime LastActivityDate { get; set; }

    public virtual ICollection<Role> Roles { get; set; }
}

- 角色

    public partial class Role
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)] 
    public int RoleId { get; set; }
    public string RoleName { get; set; }
    public string LoweredRoleName { get; set; }
    public string Description { get; set; }

    public virtual ICollection<User> Users { get; set; }
}



控制者-用户:

        public JsonResult GridData(int rows, int page)
    {
        var count = _context.Users.Count();

        var pageData = _context.Users.OrderBy(x => x.UserId).Skip((page - 1) * rows).Take(rows);

        return Json(new {
            page = page,
            records = count,
            rows = pageData,
            total = Math.Ceiling((decimal)count / rows)
        }, JsonRequestBehavior.AllowGet);
    }



查看-Index.cshtml

@model IEnumerable<CustomControler.Models.User>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<table id="ajaxGrid"></table>
<div id="ajaxGridPager"></div>

<script type="text/javascript">
    jQuery("#ajaxGrid").jqGrid({
        url: '@Url.Action("GridData")',
        datatype: "json",
        mtype: 'GET',
        colNames: ['UserId', 'UserName', 'LoweredUserName', 'MobileAlias', 'IsAnonymous', 'LastActivityDate'],
        colModel: [
            { name: 'UserId', index: 'UserId', editable: true, sortable: false, hidden: true },
            { name: 'UserName', index: 'UserName', editable: true, sortable: false, hidden: false },
            { name: 'LoweredUserName', index: 'LoweredUserName', editable: true, sortable: false, hidden: false },
            { name: 'MobileAlias', index: 'MobileAlias', editable: true, sortable: false, hidden: false },
            { name: 'IsAnonymous', index: 'IsAnonymous', editable: true, sortable: false, hidden: false },
            { name: 'LastActivityDate', index: 'LastActivityDate', editable: true, sortable: false, hidden: false }
        ],**
        rowNum: 5,
        pager: '#ajaxGridPager',
        width: '850',
        height: '15em'
    });
    //jsonReader: { repeatitems: false, id: "UserId" },
    jQuery("#ajaxGrid").jqGrid('navGrid', '#ajaxGridPager',
        { search: false, refresh: false },                     // General options
        { url: '@Url.Action("Edit")', closeAfterEdit: true },  // Edit options
        { url: '@Url.Action("Create")', closeAfterAdd: true }, // Add options
        { url: '@Url.Action("Delete")' }                       // Delete options                           
    );    
</script>



母版页

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=9" />

    <title>@ViewBag.Title</title>

    <link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
    <script src="@Url.Content("~/Scripts/jquery-1.5.2.min.js")" type="text/javascript"></script>

    <!-- To support jqGrid -->
    <link href="@Url.Content("~/Content/themes/base/jquery.ui.all.css")" rel="stylesheet" type="text/css" />
    <link href="@Url.Content("~/Content/ui.jqgrid.css")" rel="stylesheet" type="text/css" />
    <script src="@Url.Content("~/Scripts/jquery-ui-1.8.11.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/i18n/grid.locale-en.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.jqGrid.min.js")" type="text/javascript"></script>



</head>

如果我在我的User 模型上将评论设置为public virtual ICollection &lt;Role&gt; Roles {get; set;},这意味着我摆脱了与表的角色关系,数据可以是 在 jqGrid 中加载。

【问题讨论】:

    标签: jquery asp.net-mvc-3 jqgrid many-to-many


    【解决方案1】:

    由 tpeczek 解决/回答:

    问题不在于 jqGrid,而在于 JavaScriptSerializer 试图序列化整个对象树(并迷失在其中),最简单的方法是使用匿名对象(不完美,因为您必须键入想要的属性发送,但会成功)

    见:forums.asp.net

    希望这对其他人有所帮助

    【讨论】:

      猜你喜欢
      • 2011-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-13
      • 2021-11-03
      • 1970-01-01
      • 2021-02-11
      • 1970-01-01
      相关资源
      最近更新 更多