使用存储过程绑定Kendo Grid数据源后网格为空,代码缺失什么?
Hey there! Let's figure out why your Kendo Grid is coming up empty even after wiring up your stored procedure data. I’ve gone through your code and have a few key areas to check:
1. First, Verify Your Stored Procedure Returns Data
Start with the basics: make sure db.sps_selectemp() is actually pulling records from your database.
- Add a breakpoint in your
Getusercontroller method and inspect theemployeesvariable. If it’s empty, the issue is with your stored procedure or database connection—not the Kendo Grid. - Run the
sps_selectempprocedure directly in your database tool (like SSMS) to confirm it returns the expected employee records.
2. Add a Primary Key to Your DataSource Model
Kendo Grid’s Ajax data source relies on a unique identifier to properly handle data binding, even for read-only scenarios. Your sps_selectemp_Result class has an EmpID field—use it as the model ID:
Modify your view’s DataSource configuration like this:
.DataSource(dataSource => dataSource .Ajax() .Model(model => model.Id(p => p.EmpID)) // Add this line to define the primary key .Read(read => read.Action("Getuser", "Home")) .PageSize(20) )
3. Inspect the Ajax Request/Response
Open your browser’s DevTools (F12) and go to the Network tab. Trigger the grid’s data load, then look for the Getuser request:
- Check if the request returns a 200 OK status.
- Verify the response JSON matches the Kendo
DataSourceResultformat (it should includeDataandTotalproperties with your employee data insideData). - If there’s an error (like 500 Internal Server Error), click into the request to see the detailed exception message—this will point you to issues in your controller or database logic.
4. Ensure Entity-Storage Procedure Field Matching
Double-check that the column names returned by sps_selectemp exactly match the property names in your sps_selectemp_Result class (case sensitivity can vary by EF configuration, but keeping them identical avoids mapping issues). For example, if your procedure returns firstname but your property is FirstName, EF might fail to map the value.
5. Confirm Kendo UI Script/CSS References Are Complete
Missing core Kendo files can break grid functionality silently. Make sure your layout or view includes these required references:
<link href="@Url.Content("~/Content/kendo.common.min.css")" rel="stylesheet" /> <link href="@Url.Content("~/Content/kendo.default.min.css")" rel="stylesheet" /> <script src="@Url.Content("~/Scripts/jquery.min.js")"></script> <!-- Required dependency --> <script src="@Url.Content("~/Scripts/kendo.all.min.js")"></script> <script src="@Url.Content("~/Scripts/kendo.aspnetmvc.min.js")"></script>
Start with the first check (confirming stored procedure data) and work your way down—this should help you pinpoint why the grid isn’t displaying your employees.
内容的提问来源于stack exchange,提问作者JohnMathew

