[英]500 Internal Server Error Asp.net C# Datatables
我有 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 任何人都可以幫忙嗎?
首先,我認為在 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.
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.