Asp.net-Mvc

要允許 GET 請求,請將 JsonRequestBehavior 設置為 AllowGet

  • February 13, 2017

我在 Kendo UI 網格中綁定了批量記錄。響應從 Json 返回。

使用以下格式時出現錯誤:

問題程式碼:方法1:

public JsonResult KendoserverSideDemo(int pageSize, int skip=10)
{
 using (var s = new KendoEntities())
 {
   var total = s.Students.Count();

   if (total != null)
   {
     var data = s.Students.OrderBy(x=>x.StudentID).Skip(skip)
                          .Take(pageSize).ToList();

     return Json(new { total = total, 
                       data = data,
                       JsonRequestBehavior.AllowGet });
   }
   else
   {
     return null;
   }
 }
}

方法2:使用此方法可以正常工作:

public JsonResult KendoserverSideDemo(int pageSize, int skip=10)
{
 using (var s = new KendoEntities())
 {
   var total = s.Students.Count();

   if (total != null)
   {
     var data = s.Students.OrderBy(x=>x.StudentID).Skip(skip)
                          .Take(pageSize).ToList();

     return Json(data, JsonRequestBehavior.AllowGet);
   }
   else
   {
     return null;
   }
 }
}

第一種方法1有什麼問題?

您有簡單的拼寫錯誤/語法錯誤

return Json(new { total = total, data = data,JsonRequestBehavior.AllowGet });

JsonRequestBehavior.AllowGet是第二個參數Json- 它不應該是對象的一部分

return Json(new { total = total, data = data }, JsonRequestBehavior.AllowGet);

引用自:https://stackoverflow.com/questions/42206512