简体   繁体   English

KendoUI ASP.Net MVC网格-如何添加行客户端?

[英]KendoUI ASP.Net MVC Grid - How to add row client side?

I am trying to implement a very simple grid with ability to add a new row via a toolbar button. 我试图实现一个非常简单的网格,能够通过工具栏按钮添加新行。 None of the samples provided in the documentation seem to work as expected. 文档中提供的所有示例似乎均未达到预期的效果。

@model Models.MyModel

@{
    ViewBag.Title = "Simple Grid";
}

<div class="container-fluid">
    <div class="row"></div>
    <div class="row">
        <div class="col-xs-11">
            @(Html.Kendo().Grid<Models.MyModel>()
            .Name("myGrid")
            .ToolBar(toolbar => {
                toolbar.Create().Text("Add New Row");
            })
            .Columns(columns => {
                columns.Bound(p => p.Name).Width(200);
                columns.Bound(p => p.Header1).Width(100);
                columns.Bound(p => p.Header2).Width(100);
            })
            .Scrollable()
            .Editable(ed => ed.Mode(GridEditMode.InLine).CreateAt(GridInsertRowPosition.Bottom))
            .HtmlAttributes(new { style = "height:350px;" })
            .DataSource(ds => ds
                .Custom()
                .Schema(schema => schema
                        .Model(model => {
                            model.Id("Id");
                            model.Field("Name", typeof(string));
                            model.Field("Header1", typeof(string));
                            model.Field("Header2", typeof(string));
                        })
                    )
                )
            )
        </div>
    </div>
</div>

The above is in a simple index.chtml page which uses a layout page and injects all the jQuery and KendoUI scripts. 上面是一个简单的index.chtml页面,它使用布局页面并注入所有jQuery和KendoUI脚本。

When the page is loaded get a JS error Unable to get property 'nodeName' of undefined or null reference 加载页面时出现JS错误无法获取未定义或空引用的属性“ nodeName”

Not sure why that happens, but hitting continue, displays an empty grid as expected. 不确定为什么会发生这种情况,但是单击继续,将显示预期的空白网格。

Clicking the "Add new Row" toolbar button results in another JS error (same as above) 单击“添加新行”工具栏按钮会导致另一个JS错误(与上面相同)

Question: Am I missing a configuration option on the grid? 问题:我是否在网格上缺少配置选项? Per documentation, this is supposed to work "out of the box". 根据文档,这应该是“开箱即用”的。 All I want to achieve is a simple grid which adds a new empty row everytime I click the "Add" button. 我要实现的只是一个简单的网格,每次单击“添加”按钮时,该网格都会添加一个新的空行。

While it can be a bit tough to see at first, using the Custom() configuration requires more than just setting the .Schema() configuration. 虽然一开始很难看,但是使用Custom()配置不仅仅需要设置.Schema()配置。 You can refer to samples in in the Custom DataSource documentation article for references to this. 您可以参考“ 自定义数据源”文档文章中的示例以获取对此的引用。 Every sample has at least the Transport field defined with a read configuration set so that the DataSource knows where to read the data. 每个样本至少具有定义为read配置集的传输字段,以便数据源知道从何处读取数据。

Since you're doing CRUD operations here I also recommend that you set the other transport fields (Create, Read, Update, Destroy) to ensure that you can work with your controller and communicate with your backend properly. 由于您在此处执行CRUD操作,因此我还建议您设置其他传输字段(创建,读取,更新,销毁),以确保您可以使用控制器并与后端正确通信。

As a quick note, if you search around the Telerik site for the error unable to get property 'nodeName' of undefined or null reference you start to get the hint that this error is due to an incorrect configuration of a component. 快速说明一下,如果您在Telerik站点附近搜索unable to get property 'nodeName' of undefined or null reference错误,则会开始提示该错误是由于组件配置错误所致。 It's not specific to the Grid, but it does pop up from time-to-time and the issue almost always boils down to configuration options. 它不是特定于Grid的,但确实会不时弹出,并且问题几乎总是归结为配置选项。

Now, to fix this particular issue I'd recommend working with Ajax binding rather than custom binding. 现在,要解决此特定问题,我建议您使用Ajax绑定而不是自定义绑定。 Ajax binding is super simple to work with and is perfect for this kind of scenario where you want a simple Grid added to the page without the more advanced configuration that comes from working with a more advanced feature. Ajax绑定非常易于使用,非常适合这种情况,在这种情况下,您希望将简单的Grid添加到页面中,而无需使用更高级的功能来获得更高级的配置。 Plus you can work with your client-side model to set up validation etc., for your server-side (no need to set the schema manually). 另外,您可以与客户端模型一起为服务器端设置验证等(无需手动设置架构)。

Or, alternatively just set the transport field configuration to valid ActionResults or URLs to push information back and forth properly. 或者,也可以仅将传输字段配置设置为有效的ActionResults或URL,以正确地来回推送信息。

When first implementing any new product I always recommend following documentation closely and starting off with the smaller examples and building form there. 首次实施任何新产品时,我总是建议您仔细阅读以下文档,并从较小的示例开始并在那里建立表单。 If you're looking to work with a custom data source then I say start with a bigger configuration and remove pieces until you get the minimal piece that you're looking for. 如果您要使用自定义数据源,那么我想说的是从更大的配置开始,然后删除片段,直到获得所需的最小片段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM