asp.net mvc - c# mvc Kendo Grid不显示数据,但似乎知道它

本文关键字:mvc 数据 显示 net Kendo Grid asp | 更新日期: 2023-09-27 17:54:51

所以我试图实现一个剑道MVC网格到一个大学的任务,我的问题是,网格说"没有数据",但当我点击在一个列名它显示我一些文本包含的数据应该在网格中。图片:

My Grid in My browser window

单击其中一个列名时显示的内容

我代码:

Controller for The Grid:

public class KurseSuchenGridController : Controller
{
private ApplicationDbContext db = new ApplicationDbContext();
public ActionResult KurseSuchenGrid()
{
    return View();
}
public ActionResult Kurse_Read([DataSourceRequest]DataSourceRequest request)
{
    IQueryable<Kurs> kurse = db.Kurse;
    DataSourceResult result = kurse.ToDataSourceResult(request, kurs => new {
        Id = kurs.Id,
        Titel = kurs.Titel,
        Inhaltsbeschreibung = kurs.Inhaltsbeschreibung,
        Agenda = kurs.Agenda,
        Ortsangabe = kurs.Ortsangabe,
        Termin = kurs.Termin,
        Anmeldeschluss = kurs.Anmeldeschluss,
        //gelistet = kurs.gelistet,
        ApplicationUserId = kurs.ApplicationUserId
    });
    return Json(result, JsonRequestBehavior.AllowGet);
}

protected override void Dispose(bool disposing)
{
    db.Dispose();
    base.Dispose(disposing);
}

我的观点:

@(Html.Kendo().Grid<Plattform.Models.Kurs>()
      .Name("grid")
      .Columns(columns =>
      {
        columns.Bound(c => c.Titel);
        columns.Bound(c => c.Inhaltsbeschreibung);
        columns.Bound(c => c.Agenda);
        columns.Bound(c => c.Ortsangabe);
        columns.Bound(c => c.Termin);
        columns.Bound(c => c.Anmeldeschluss);
        columns.Bound(c => c.ApplicationUserId);
      })
      .Pageable()
      .Sortable(sortable => {
          sortable.SortMode(GridSortMode.SingleColumn);
      })
      .Filterable(filterable => filterable.Mode(GridFilterMode.Row))
      .Scrollable()
      .DataSource(dataSource => dataSource
          .Ajax()
          .Read(read => read.Action("Kurse_Read", "KurseSuchenGrid"))
          .PageSize(20)
      )
)

asp.net mvc - c# mvc Kendo Grid不显示数据,但似乎知道它

您似乎正在做服务器操作。如果是这样,您可能想要添加ServerOperation(true),也添加Model(model => model.Id(m => m.Id)),并添加[HttpPost]到动作方法。

.DataSource(dataSource => dataSource
   .Ajax()
   .ServerOperation(true) <=====
   .Model(model => model.Id(m => m.Id)) <=====
   .Read(read => read.Action("Kurse_Read", "KurseSuchenGrid"))
   .PageSize(20)
      )
[HttpPost] <=====
public ActionResult Kurse_Read([DataSourceRequest]DataSourceRequest request)
{
   ...
}

像这样添加模型:

.Model(model =>
    {
        model.Id(b => b.field1);
        model.Field(b => b.field2);
        model.Field(b => b.field3);
        model.Field(b => b.field4);
        model.Field(b => b.field5);
        ...
    })