在 kendo 网格 mvc 中抑制读取操作

Suppress read action in kendo grid mvc

我如何抑制 kendo 网格 mvc 中的读取操作,它取决于发送到该网格的模型中的字段?

例如,我发送了错误的值并且没有调用读取操作

正如 JamieD77 提到的那样,其中一种方法是使用网格(而非数据源)的自动绑定选项:

@(Html.Kendo().Grid<OrderViewModel>()
   .Name("grid")
   .AutoBind(Model.ShouldLoad)

然而,使用上述方法有一些缺点 - 例如,如果启用了网格的 "Sortable" 选项并且用户尝试对空网格进行排序,它将导致 "Read" 请求(网格将尝试从服务器获取数据)。这就是为什么我建议 ot 而不是有条件地隐藏整个 Grid 或有条件地删除 DataSource 的 "Read" 选项并使用它的 "BindTo" 选项将 Grid 绑定到空数组。

我还需要根据模型中的值有条件地抑制网格的读取操作(在这种情况下没有理由调用服务器)。感谢@Vladimir lliev 的回答,他提到不使用 AutoBind,而是从 DataSource 中删除 "Read" 操作并绑定到一个空数组。

这为我指明了正确的方向,但我不知道如何使用剃刀语法来做到这一点。我弄明白了,所以我分享给任何需要它的人。

@(Html.Kendo().Grid<SomeNamespace.Model>(
    // If you need to optionally bind to an empty datasource in certain scenarios,
    // use the grid's constructor. Also, conditionally enable the DataSource's "Read"
    // action. Note: it's not enough to just conditionally enable the "Read" action,
    // since the grid still makes a request for some reason, but when you use an empty
    // array AND disable the "Read" action, no call is made to the server.
    Model.ShouldGridRead ? null : new SomeNamespace.Model[] { }
)
.Name("MyGrid")
.Columns(cols =>
{
})
.DataSource(ds => ds
    .Ajax()
    .Batch(true)
    .Model(model =>
    {
    })
    .Read(a =>
    {
        if (Model.ShouldGridRead)
        {
            a.Action("Some_Action", "Some_Controller");
        }
    })
)