具有Func属性的JsonConvert SerializeObject

时间:2016-05-04 15:53:29

标签: asp.net-mvc razor json.net

我的剃刀视图我有模板像下面的

@helper EditLink(SampleEmployeeModel data)
    {
    <b>
        @Html.ActionLink("Edit", "Edit", new { id =data.ID }, new { @class = "modal_link" })
    </b>
}

然后我在网格控件中使用此模板,如下图所示。 Grid Helper方法基于JQuertDataTable工作。

@(
        Html.Grid<SampleEmployeeModel>()
        .SetName("GridEmployeeServerSide")
        .Columns(columns =>
        {
            columns.Bound(b => b.ID).EnableSearch(false); 
            columns.Bound().Template(o => EditLink(o)).SetTitle("Edit");
        })
        .Pageable(a => a.SetPageType(PageType.full_numbers))
        .DataSource(a =>
        {
            a.Read(read => read.Action("RRDLDataTableAjaxData"));
            a.ServerSideProcessing(true);
        })
    )

RRDLDataTableAjaxData应该像下面那样返回记录

 [
    {"ID":1,"Edit":"<b>\r\n<a class=\"modal_link\" href=\"/Samples/Edit/1\">Edit</a>\r\n</b>\r\n"},
    {"ID":2,"Edit":"<b>\r\n<a class=\"modal_link\" href=\"/Samples/Edit/2\">Edit</a>\r\n</b>\r\n"},
    {"ID":3,"Edit":"<b>\r\n<a class=\"modal_link\" href=\"/Samples/Edit/3\">Edit</a>\r\n</b>\r\n"}
 ]

第一次网格加载时我得到了预期的结果,如果我转到第二页则不返回预期的JSon。

Helper方法如何生成HTML Grid 它将波纹管代码写入Razor视图

 <table class="table table-striped nowrap dt-responsive" data-rrdl-grid="" id="RRDLGrid21V52" width="100%">
        <thead>
            <tr>
                <TH></TH>  
                <TH></TH>
            </tr>
        </thead>
        <tbody></tbody>
    </table> 

    <script type='text/javascript'>
    var dataTable_GridEmployeeServerSide_Option =  {
  "columns": [
    {
      "targets": 1,
      "defaultContent": "",
      "data": "ID",
      "title": "ID",
      "visible": true
    },   
    {
      "targets": 2,
      "defaultContent": "",
      "data": "Edit",
      "searchable": true,
      "title": "Edit",
      "visible": true
    }
  ],
  "dom": "<'table-header-row' <'dom-left' ><'dom-center' ><'dom-right' lp>>t",
  "autoWidth": false,
  "deferRender": true,
  "info": false,
  "lengthChange": false,
  "ordering": true,
  "paging": true,
  "processing": false,
  "searching": false,
  "serverSide": true,
  "stateSave": false,
  "ajax": {
    "contentType": "application/json; charset=utf-8",
    "url": "../Samples/RRDLDataTableAjaxData",
    "dataType": "json",
    "dataSrc": "data"
  },
  "destroy": false,
  "orderClasses": false,
  "orderMulti": false,
  "pageLength": 10,
  "pagingType": "full_numbers",
  "scrollCollapse": false,
  "language": {
    "paginate": {
      "first": "<i class=\"icon-left\"></i><i class=\"icon-left\"></i>",
      "last": "<i class=\"icon-right\"></i><i class=\"icon-right\"></i>",
      "next": "<i class=\"icon-right\"></i>",
      "previous": "<i class=\"icon-left\"></i>"
    }
  },
  "OddRowColor": null,
  "EvenRowColor": null,
  "FixedRowHeight": 0,
  "HighlightSearchText": false,
  "SelectCheckBox": false,
  "RowTemplate": null
}
    $(document).ready(function () {
_DataTable(GridEmployeeServerSide);}
)
    </script>

我的问题是如何在用户点击第二页时将 EditLink 传递给帮助服务器。

1 个答案:

答案 0 :(得分:0)

错误显示负责错误的不同属性。显然它是ApplicationInstance导致循环引用。它与提供的代码段无关。

根据Json.Net文档,您可以使用以下设置避免循环引用问题:

var jsonSerializerSettings = new JsonSerializerSettings
{
    PreserveReferencesHandling = PreserveReferencesHandling.Objects
};