【问题标题】:500 Internal Server Error Asp.net C# Datatables500 内部服务器错误 Asp.net C# 数据表
【发布时间】:2019-03-01 22:35:42
【问题描述】:

我有 MVC 5 asp.net C# 项目库存和餐厅,当我在项目中使用 Datatable 时,这是我的 html:

   <table class="table table-striped table-bordered table-hover" id="dataTables-example">
        <thead>
            <tr>
                <th>
                    @Resources.Tokens_Admin.Name
                </th>
                <th>
                    @Resources.Tokens_Admin.Category
                </th>
                <th>
                    @Resources.Tokens_Admin.Price
                </th>
                <th>
                    @Resources.Tokens_Admin.Image
                </th>
                <th>

                </th>
             </tr>
        </thead>
        <tfoot>
        </tfoot>
    </table>

和 JavaScript:

<script>
    $(document).ready(function () {
        $("#dataTables-example").DataTable({
            "ajax": {`enter code here`
                "url": "/Meals/GetMealsList",
                "type": "POST",
                "datatype": "json"
            },
            "columns": [
                   { "data": "Name", "name": "Name" },
                   { "data": "CatId", "name": "CatId" },                      
                   { "data": "Price", "name": "Price" },
                   { "data": "Image", "name": "Image" },
                   {
                       "data": "Id", "render": function (Id, type, full, meta) {
                           debugger
                           return '<a href="#" onclick="Edit(' + Id + ')"><i class="glyphicon glyphicon-pencil"></i></a>'
                  }

                   }
            ],
            "serverSide": "true",
            "order": [0, "asc"],
            "processing": "true",
            "language": {
                "processing": "processing...please wait"
            }
        });
    });
</script>

和控制器:

  [HttpPost]
          public ActionResult GetMealsList()
    {
        // server-side parameters
        int start = Convert.ToInt32(Request["start"]);
        int length = Convert.ToInt32(Request["length"]);
        string searchval = Request["search[value]"];
        string sortColumnName = Request["columns[" + Request["order[0][column]"] + "][name]"];
        string sortDirection = Request["order[0][dir]"];
        var MealList = db.Meals.ToList();
        int totalrows = MealList.Count();
        if (!string.IsNullOrEmpty(searchval))
        {
            MealList = MealList.Where(x => x.Price.ToString().Contains(searchval.ToLower()) || x.Category.Name.ToLower().Contains(searchval.ToLower())
            || x.Name.ToLower().Contains(searchval.ToLower())).ToList();
        }
        int totalrowsafterfiltering = MealList.Count();
        // sorting
        MealList = MealList.OrderBy(sortColumnName + " " + sortDirection).ToList();

        //paging
        MealList = MealList.Skip(start).Take(length).ToList();

        return Json(new { data = MealList, draw = Request["draw"], recordsTotal = totalrows, recordsFiltered = totalrowsafterfiltering }, JsonRequestBehavior.AllowGet);
    }

当我运行视图索引时,我遇到了内部服务器错误 500。 无法找到该资源。 说明:HTTP 404。您要查找的资源(或其依赖项之一)可能已被删除、名称已更改或暂时不可用。请查看以下 URL 并确保其拼写正确。 请求的 URL:/Meals/GetMealsList 有人可以帮忙吗?

【问题讨论】:

  • A 404 和 500 错误是两个非常不同的事情,因此有不同的原因。你收到的是哪一个? 500 表示服务器错误(通常是某种崩溃),而 404 只是说明无法找到请求的资源。 404表示服务器崩溃。尝试查看不同的错误代码并澄清您的问题。如果您确实收到 500 错误,请发布错误消息。否则很难提供帮助。
  • GetMealsList 所在的类是用什么装饰的?

标签: javascript c#


【解决方案1】:

首先我想在 Javascript 中改变你的行:

"url": "/Meals/GetMealsList",

"url": "@Url.Action("GetMealsList", "Meals")",

应该修复它。我假设您的控制器名为MealsController?

其次,我建议您执行以下操作,在其中创建代表数据表传回的数据的 ViewModel,这应该比您引用 Request 对象的方式更轻松应该使用模型绑定将 json 数据映射到您的模型。

注意,你不应该需要 Newtonsoft.Json.JsonProperty 行,我只是从一个项目中复制了这段代码(与 webapi 相关)。

    public class DataTablesSearchModel
    {
        // properties are not capital due to json mapping
        [Newtonsoft.Json.JsonProperty(PropertyName = "draw")]
        public int Draw { get; set; }

        [Newtonsoft.Json.JsonProperty(PropertyName = "start")]
        public int Start { get; set; }

        [Newtonsoft.Json.JsonProperty(PropertyName = "length")]
        public int Length { get; set; }

        [Newtonsoft.Json.JsonProperty(PropertyName = "columns")]
        public List<Column> Columns { get; set; }

        [Newtonsoft.Json.JsonProperty(PropertyName = "search")]
        public Search Search { get; set; }

        [Newtonsoft.Json.JsonProperty(PropertyName = "order")]
        public List<Order> Order { get; set; }
    }

    public class Column
    {
        [Newtonsoft.Json.JsonProperty(PropertyName = "data")]
        public string Data { get; set; }

        [Newtonsoft.Json.JsonProperty(PropertyName = "name")]
        public string Name { get; set; }

        [Newtonsoft.Json.JsonProperty(PropertyName = "searchable")]
        public bool Searchable { get; set; }

        [Newtonsoft.Json.JsonProperty(PropertyName = "orderable")]
        public bool Orderable { get; set; }

        [Newtonsoft.Json.JsonProperty(PropertyName = "search")]
        public Search Search { get; set; }
    }

    public class Search
    {
        [Newtonsoft.Json.JsonProperty(PropertyName = "value")]
        public string Value { get; set; }

        [Newtonsoft.Json.JsonProperty(PropertyName = "regex")]
        public string Regex { get; set; }
    }

    public class Order
    {
        [Newtonsoft.Json.JsonProperty(PropertyName = "column")]
        public int Column { get; set; }

        [Newtonsoft.Json.JsonProperty(PropertyName = "dir")]
        public string Dir { get; set; }
    }

那么你的控制器动作如下所示:

[HttpPost]
      public ActionResult GetMealsList(DataTablesSearchModel model)
{
     //Access model for the data passed by datatables.
}

【讨论】:

  • 我把Ajax里面的Url改成了"url":"@Url.Action("GetMealsList", "Meals")",问题还是没有解决:(
  • @BaselMariam 也试试我对模型的建议?可能是 MVC 找不到操作方法,因为它正在寻找的签名与它正在发布对象的事实不匹配。您是否从控制台收到您在 html 中遇到的错误,或者当您第一次导航到它时整个页面都显示该错误?
  • @BaselMariam 这个页面的 HttpGet 方法是什么样的?
  • 整个页面显示该错误错误是:“DataTables 警告:table id=dataTables-example - Ajax 错误。有关此错误的更多信息,请参阅datatables.net/tn/7
猜你喜欢
  • 1970-01-01
  • 2012-06-22
  • 1970-01-01
  • 1970-01-01
  • 2012-10-27
  • 2016-06-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多