【问题标题】:Kindo Grid in MVC is displaying raw dataMVC 中的 Kendo Grid 显示原始数据
【发布时间】:2020-01-07 16:30:17
【问题描述】:

我是 Kendo 新手,正在学习如何将它与 MVC 集成并在网格中显示数据。

我的控制器

[HttpGet]
public ActionResult StudentList([DataSourceRequest]DataSourceRequest request)
{
    DataSourceResult result = 
    _acc.GetStudent(StudId).ToDataSourceResult(request,
        model => new StudentModel
        {
            ID = model.ID,
            StudId = model.StudId,
            Name= model.Name,
            Email= model.FullName,
            custEmail = model.Email
        });


        return Json(result, JsonRequestBehavior.AllowGet);
}

我的看法

@(Html.Kendo().Grid<Models.StudentModel>()
                .Name("grid")
                .Columns(columns =>
                {
                    columns.Bound(c => c.StudId);
                    columns.Bound(c => c.Name);
                    columns.Bound(c => c.Email);
                })
                     .Pageable()
                     .Sortable()
                     .Filterable()
                     .Scrollable(scr => scr.Height(550))
                     .DataSource(dataSource => dataSource
                     .Ajax()
                     .Read(read => read.Action("StudList", "Student"))
                     .ServerOperation(false)
  )
)

我在浏览器中得到的输出看起来像

{"Data":[{"ID":1102,"StudId":4006,"Name":"Adam Lyon","Email":"alyon@regionofwaterloo.ca",",....,

有谁知道为什么数据没有以网格形式格式化?

【问题讨论】:

  • 您是否收到控制台错误?您的代码缺少 DataSource(... 的右括号)
  • DataSource 已经关闭,它在下面,没有错误,所有数据都来了,但格式不正确,我想要它在一个网格中,但是看到上面的输出我有

标签: c# asp.net-mvc kendo-ui kendo-grid kendo-asp.net-mvc


【解决方案1】:

如果您直接链接到 EmployeeList 操作,您将获得该行为 - 只能由网格调用。如果您的视图名称是 Index,您将需要另一个操作:

public ActionResult Index()
{
    return View();
}

然后在代码链接中:

@Html.ActionLink("Employee List", "Index", "Employee")

现在将加载视图并渲染 Kendo 网格,然后调用您的 EmployeeList 操作来填充网格。

请参阅剑道示例控制器here。它有一个用网格加载视图的操作,然后网格将通过 AJAX 调用 CRUD 操作。

【讨论】:

  • 对不起,我应该在哪里包含代码行@Html.ActionLink("Employee List", "Index", "Employee")?
  • 请帮助我们如何使其正常工作。我创建了一个单独的索引视图并包含了上述操作。现在它得到500 - Internal server error.
  • 您可以从您的菜单 (_Layout)、主页上的链接等调用索引控制器操作。如果您收到 500 错误(服务器错误),很可能在您的阅读范围内行动。所以设置一个调试点,看看你是否击中它。此外,我提供的操作链接只是一个示例。您的实际链接将取决于您的控制器名称。在此示例中,控制器将是“EmployeeController”。使用语法@Html.ActionLink("Label","ActionName","ControllerName")。
猜你喜欢
  • 1970-01-01
  • 2015-03-22
  • 1970-01-01
  • 1970-01-01
  • 2014-09-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-20
相关资源
最近更新 更多