1

我将 URL 转换为用户友好的,但问题是,它导致我的 AJAX 无法正常工作:它没有返回 JSON,而是响应text/html; charset=utf-8,知道吗?

本地主机/家/票?tab=0

本地主机/家/票/0

jQuery 数据表

self.$('#tblticket').dataTable({
    "bDestroy": true,
    "bServerSide": true,
    "sAjaxSource": '/Home/Ticket/AjaxHandler',
    "fnServerParams": function (aoData){
     aoData.push( { "name": "sStatus", "value": status } );
 },

路由器

routes.MapRoute(
   name: "TicketRoute",
   url: "Ticket/{tab}",
   defaults: new { controller = "Ticket", action = "Index", tab = UrlParameter.Optional }
);

更新 1

如果我将路由器更改为此,它可以工作,但 url 看起来像 localhost/Home/Ticket/Index/0。路由器干扰了ajax调用,知道吗?我希望 url 看起来像localhost/Home/Ticket/0并且仍然能够进行 ajax 调用,我不知道,可以做什么?

        routes.MapRoute(
            name: "TicketRoute",
            url: "Ticket/Index/{tab}",
            defaults: new { controller = "Ticket", action = "Index", tab = UrlParameter.Optional }
        );
4

1 回答 1

2

在我的 MVC4 版本中,我执行了以下操作:

  1. 在我的控制器中创建了一个 DataTables 可以访问的方法

    [HttpGet]
    public ActionResult GetRecords(jQueryDataTableParamModel param)
    {
        #region ViewModel Binding with TotalRecords count, pagination and search
        List<vm_DataTable_TableName> model;
        int totalRecords = 0;
        using (DBEntities db = new DBEntities())
        {
            model = Mapper.Map(db.GetRecords(), new List<vm_DataTable_TableName>()); //Using AutoMapper for viewmodel bindings
            totalRecords = model.Count();
        }
        #endregion
    
        #region Filtration/Pagination
        var filtered = ***Filter depending on your needs***
    
        var displayed = filtered.Skip(param.iDisplayStart).Take(param.iDisplayLength);
        #endregion
    
        #region Convert Result to String Array for JSON Conversion/Trim excess white space to reduce size of result
        IEnumerable<string[]> result = new List<string[]>();
        result = displayed
                 .Select(s => new string[] {
                    (!string.IsNullOrEmpty(Convert.ToString(s.ID))) ? s.ID.ToString().Trim() : string.Empty,
                    (!string.IsNullOrEmpty(s.val_1)) ? s.val_1.Trim() : string.Empty,
                    (!string.IsNullOrEmpty(s.val_2)) ? s.val_2.Trim() : string.Empty,
                    (!string.IsNullOrEmpty(s.val_3)) ? s.val_3.Trim() : string.Empty
                 });
        #endregion
    
        return Json(new
        {
            sEcho = param.sEcho,
            iTotalRecords = totalRecords,
            iTotalDisplayRecords = filtered.Count(),
            aaData = result
        },
        JsonRequestBehavior.AllowGet);
    }
    
  2. 在 jQuery DataTables 初始化中:

    $('#tblticket').dataTable({
        "bDestroy": true,
        "bServerSide": true,
        "sAjaxSource": '@Url.Content("~/Home/Ticket/GetRecords")', //Server inserted url
        "fnServerData": function Data(sSource, aoData, fnCallback) {
            $.ajax({
                "dataType": "json",
                "contentType": "application/json; charset=utf-8",
                "type": "GET",
                "url": sSource,
                "data": aoData,
                "success":
                    function (msg) {
                        //Whatever other actions you want to do with the data
                        fnCallback(msg);
                    },
                "failure":
                    function (XMLHttpRequest, textStatus, errorThrown) {
                        alert(XMLHttpRequest.status);
                        alert(XMLHttpRequest.responseText);
                    }
            });
        }
    });
    

'@Url.Content("")' 在使用 MVC 区域时也很友好。

当我第一次开始使用这个插件时,这些系列文章对我帮助很大。

于 2013-08-21T15:09:15.280 回答