C#MVC Kendo Grid不显示数据,但似乎知道它

时间:2016-07-05 15:11:48

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

所以我试图将Kendo MVC Grid实现到大学任务中,我的问题是网格显示“No Data”但是当我点击其中一个列名时它会显示一些包含应该包含数据的文本在网格中。图片:

My Grid in my browser window

What gets displayed when I click one of the column names

我的代码:

网格控制器:

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)
      )
)

2 个答案:

答案 0 :(得分:1)

看来你正在做服务器操作。如果是,您可能需要添加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)
{
   ...
}

答案 1 :(得分:-1)

添加如下模型:

.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);
        ...
    })