cshtml文件:(home.cshtml)
@using Kendo.Mvc.UI;
<!DOCTYPE html>
<html>
<head>
<title>Welcome to Kendo UI!</title>
<!-- Common Kendo UI CSS for web widgets and widgets for data visualization. -->
<link href="~/Kendo/styles/kendo.common.min.css" rel="stylesheet" />
<!-- (Optional) RTL CSS for Kendo UI widgets for the web. Include only in right-to-left applications. -->
<link href="~/Kendo/styles/kendo.rtl.min.css" rel="stylesheet" type="text/css" />
<!-- Default Kendo UI theme CSS for web widgets and widgets for data visualization. -->
<link href="~/Kendo/styles/kendo.default.min.css" rel="stylesheet" />
<!-- (Optional) Kendo UI Hybrid CSS. Include only if you will use the mobile devices features. -->
<link href="~/Kendo/styles/kendo.default.mobile.min.css" rel="stylesheet" type="text/css" />
<!-- jQuery JavaScript -->
<script src="~/Kendo/js/jquery.min.js"></script>
<!-- Kendo UI combined JavaScript -->
<script src="~/Kendo/js/kendo.all.min.js"></script>
</head>
<body>
@(Html.Kendo().Grid<MiCustomerSentiment.UserProfile>()
.Name("divSearchResult")
.DataSource(dataSource => dataSource // Configure the grid data source
.Ajax() // Specify that ajax binding is used
.Read(read => read.Action("Products_Read", "Home"))
)
.Columns(columns =>
{
columns.Bound(user => user.FirstName);
columns.Bound(user => user.LastName);
columns.Bound(user => user.EmployeeId);
columns.Bound(user => user.UserName);
})
.Pageable() // Enable paging
.Sortable() // Enable sorting
)
</body>
</html>
控制器中的后端操作方法:
using Kendo.Mvc.UI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MiCustomerSentiment.Models;
using Kendo.Mvc.Extensions;
namespace MiCustomerSentiment.Controllers
{
public class HomeController : Controller
{
public ActionResult Home()
{
return View();
}
[HttpGet]
public ActionResult Products_Read([DataSourceRequest]DataSourceRequest request)
{
using (var _db = new SampleData())
{
IQueryable<UserProfile> unitslist = _db.UserProfiles;
DataSourceResult result = unitslist.ToDataSourceResult(request);
return Json(result,JsonRequestBehavior.AllowGet);
}
}
}
}
型号:(UserProfile.cs)
namespace MiCustomerSentiment
{
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.Spatial;
[Table("support.UserProfile")]
public class UserProfile
{
[Key]
public int UserId { get; set; }
[Required(ErrorMessage = "First Name is required")]
[StringLength(100)]
public string FirstName { get; set; }
[Required(ErrorMessage = "Last Name is required")]
[StringLength(100)]
public string LastName { get; set; }
[Required(ErrorMessage = "Employee Id is required")]
[StringLength(100)]
public string EmployeeId { get; set; }
[Required(ErrorMessage = "UserName is required")]
[StringLength(100)]
public string UserName { get; set; }
[Required(ErrorMessage = "Password is required")]
[StringLength(50)]
public string Password { get; set; }
public bool? IsActive { get; set; }
public DateTime ModifiedDate { get; set; }
public string ModifiedBy { get; set; }
}
}
感谢您的帮助!
尝试了所有具有类似问题的链接。但是无法解决我的问题。
尝试在控制器操作方法中添加JsonBehavior.Allowget。
尝试添加[httpget]动词。
预期结果:数据必须显示在网格中。