且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Kendo Grid基于对象数组的多列组

更新时间:2023-02-02 22:11:14

您的ResultsAsync_Read方法是一种异步方法,将由Kendo框架从javascript AJAX调用中调用,即之后页面已加载并呈现.

Your ResultsAsync_Read method is an async method that will be called by the Kendo framework from a javascript AJAX call, i.e. after your page has been loaded and rendered.

这意味着呈现页面时ViewBag.Lots实际上为空,这会引发异常.

This means that when your page is rendered, ViewBag.Lots is actually null, which throw the exception.

您需要的是在加载页面时初始化此值,而不是在ResultsAsync_Read方法内部.基本上:

What you need is to initialize this value when you load the page, not inside your ResultsAsync_Read method. Basically:

public async Task<ActionResult> Index()
{
    // Gets the values BEFORE rendering the view
    IEnumerable<Result> controlSets = await _manager.ReadAsync(test);

    // The ViewBag property will be available from the Razor view            
    ViewBag.Lots = controlSets.Select(x => x.LotResults);

    // Returns the view that display the grid
    return this.View();
}

重要的是要记住MVC东西实际上是如何工作的.基本上,这些步骤是:

It's important to remember how the MVC stuff actually works. Basically the steps are:

  • 服务器在.../索引路由上收到请求
  • 服务器执行Index()操作
  • 当操作返回this.View()(与this.View("Index")等效)时,它将呈现Index.cshtml剃刀视图(这意味着ViewBag在此处不能为空)
  • 如果以后再执行诸如ResultsAsync_Read之类的AJAX调用,则更改ViewBag 不会生效,因为页面已呈现.您唯一可以做的就是修改页面,返回一些JSON,并根据JSON结果从AJAX回调内部更改DOM (即使用jQuery/javascript).
  • the server receives a request on the .../Index route
  • the server executes the Index() action
  • as the action returns this.View() (which would be equivalent to this.View("Index")), it renders the Index.cshtml razor view (that means the ViewBag must be not null here)
  • if later you do AJAX calls such as ResultsAsync_Read, changing the ViewBag won't have any effect as the page is already rendered. The only thing you could do to modify your page, is to return some JSON, and change the DOM based on the JSON result from inside your AJAX callback (i.e. using jQuery/javascript).