更新时间: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:
ResultsAsync_Read
之类的AJAX调用,则更改ViewBag 不会生效,因为页面已呈现.您唯一可以做的就是修改页面,返回一些JSON,并根据JSON结果从AJAX回调内部更改DOM (即使用jQuery/javascript).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).