如何使用asp.net mvc将数据提取到DataTable中?

时间:2017-12-02 09:20:48

标签: c# jquery asp.net-mvc datatable

我想从服务器将数据提取到asp.net mvc5中的数据表中。我有自己的数据表模板,我想自定义,但我无法获取数据。 ajax调用加上数据表本身如下所示:

var DatatableRemoteAjaxDemo = function() {
  //== Private functions

  // basic demo
  var demo = function() {

    var datatable = $('.m_datatable').mDatatable({
      // datasource definition
      data: {
        type: 'remote',
        source: {
          read: {
            // sample GET method
            method: 'GET',
            url: '/ActionLinks/getActionLinks',
            map: function(raw) {
              // sample data mapping
              var dataSet = raw;
              if (typeof raw.data !== 'undefined') {
                dataSet = raw.data;
              }
              return dataSet;
            },
          },
        },
        pageSize: 10,
        saveState: {
          cookie: true,
          webstorage: true,
        },
        serverPaging: true,
        serverFiltering: true,
        serverSorting: true,
      },

      // layout definition
      layout: {
        theme: 'default', // datatable theme
        class: '', // custom wrapper class
        scroll: false, // enable/disable datatable scroll both horizontal and vertical when needed.
        footer: false // display/hide footer
      },

      // column sorting
      sortable: true,

      pagination: true,

      toolbar: {
        // toolbar items
        items: {
          // pagination
          pagination: {
            // page size select
            pageSizeSelect: [10, 20, 30, 50, 100],
          },
        },
      },

      search: {
        input: $('#generalSearch'),
      },

      // columns definition
      columns: [
        {
          field: 'ActionLinkId',
          title: '#',
          sortable: false, // disable sort for this column
          width: 40,
          selector: false,
          textAlign: 'center',
        }, {
          field: 'ActionText',
          title: 'Quiz Title',
          // sortable: 'asc', // default sort
          filterable: false, // disable or enable filtering
          width: 150,

        }, {
          field: 'ActionDescription',
          title: 'Quiz Description',
          width: 150,

        }, {
          field: 'Category',
          title: 'Category',
        }, {
          field: 'SmallImageURL',
          title: 'Small Image URL',
          width: 100,
        }, {
          field: 'LargeImageURL',
          title: 'Large IMG URL',
          width: 100,
        },{
          field: 'DateCreated',
          title: 'CreationDate',
          sortable: 'asc',
          type: 'date',
          format: 'MM/DD/YYYY',
        }],
    });

    var query = datatable.getDataSourceQuery();

    $('#m_form_status').on('change', function() {
      // shortcode to datatable.getDataSourceParam('query');
      var query = datatable.getDataSourceQuery();
      query.Status = $(this).val().toLowerCase();
      // shortcode to datatable.setDataSourceParam('query', query);
      datatable.setDataSourceQuery(query);
      datatable.load();
    }).val(typeof query.Status !== 'undefined' ? query.Status : '');

    $('#m_form_type').on('change', function() {
      // shortcode to datatable.getDataSourceParam('query');
      var query = datatable.getDataSourceQuery();
      query.Type = $(this).val().toLowerCase();
      // shortcode to datatable.setDataSourceParam('query', query);
      datatable.setDataSourceQuery(query);
      datatable.load();
    }).val(typeof query.Type !== 'undefined' ? query.Type : '');

    $('#m_form_status, #m_form_type').selectpicker();

  };

  return {
    // public functions
    init: function() {
      demo();
    },
  };
}();

jQuery(document).ready(function() {
  DatatableRemoteAjaxDemo.init();
});

从数据库中获取数据的JsonResult代码如下所示:

public JsonResult getActionLinks()
        {
            var actionlinks = db.ActionLinks.OrderBy(i => i.ActionLinkId).ToList();
            return Json(new { data = actionlinks }, JsonRequestBehavior.AllowGet);

    }

我调试了调用点击URL但我无法将数据提取到我的数据表中。它说304错误。请帮忙。

0 个答案:

没有答案