提交表单与剑道MVC网格和其他元素(Submit form with Kendo MVC Grid

2019-07-18 19:09发布

我试图让含有剑道MVC电网等元素提交表单。

  • 视图模型包含包含三个字符串字段和IEnumerable集合。
  • 网格是服务器的约束。 我不加入任何元素或使用网格从列表中删除的任何元素,但电网包含映射到列表中的项目的布尔栏的复选框。

每当我提交此表三种元素在后方法中返回,但名单总是空。

下面是数据模型:

public class Parent
{
    public string Field1 { get; set; }
    public string Field2 { get; set; }
    public string Comments { get; set; }
    public IEnumerable<ChildItems> Children { get; set; }
}

public class ChildItems
{
    public string ChildField1 { get; set; }
    public string ChildField2 { get; set; }
    public boolean Include { get; set; }
}

这是我的看法:

@{
    ViewBag.Title = "Index";
}
@model GridInForm.Models.Parent

@using(Html.BeginForm("Save", "Home"))
{
    <fieldset>
        <legend>Editing Parent</legend>

        @Html.LabelFor(parent => parent.Field1)
        @Html.EditorFor(parent => parent.Field1)

        @Html.LabelFor(parent => parent.Field2)
        @Html.EditorFor(parent => parent.Field2)
        @Html.LabelFor(parent => parent.Comments)
        @Html.EditorFor(parent => parent.Comments)
        @(Html.Kendo().Grid(Model.Children)
            .Name("Children")
            .ToolBar(tools => tools.Create().Text("Add new Children"))
            .Editable(editable => editable.Mode(GridEditMode.PopUp).CreateAt(GridInsertRowPosition.Bottom))
            .Columns(columns =>
            {
                columns.Bound(p => p.ChildField1).ClientTemplate("#= ChildField1 #" + 
                    "<input type='hidden' name='ChildField1[#= index(data)#].ChildField1' value='#= Name #' />"
                );

                columns.Bound(p => p.ChildField2).Hidden().ClientTemplate("#= ChildField1 #" +
                    "<input type='hidden' name='ChildField1[#= index(data)#].ChildField1' value='#= ChildField1 #' />"
                );

                columns.Command(command => 
                {
                    // command.Destroy();
                    command.Edit();
                }).Width(100);
            })
            .DataSource(dataSource => dataSource
                .Server()
                .Create("Create", "Home")
                .Read("Index", "Home")
                .Update("Update", "Home")
                .Model(model => 
                {
                    model.Id(p => p.ChildField1);
                    model.Field(p => p.ChildField1).Editable(false);
                })
                //.ServerOperation(true)
            )
        )
    </fieldset>

    <input type="submit" value="Save" />
}

<script>
    function index(dataItem) {
        alert("bindind");
        var data = $("#Products").data("kendoGrid").dataSource.data();
        return data.indexOf(dataItem);
    }
</script>

当我提交表单,我得到父项背视图模型,但IEnumerable从电网领域总是空。

这只是没有做到这一点的方式,如果有什么要完成这样的事情呢? 我曾在以前的版本Telerik的这个问题,我认为这是对剑道UI一样。 任何方向将不胜感激。 这是一个长期持续的问题。

Answer 1:

我正好有这个场景在我的项目完美的工作。 这里是我的网声明...

@(Html.Kendo().Grid(Model.ChildLines)
    .Name("RequestLinesGrid")
    .Editable(editable => editable.Mode(Kendo.Mvc.UI.GridEditMode.InCell))
         .Columns(columns =>
         {
             columns.Bound(p => p.ItemCode).ClientTemplate("#= ItemCode #" +
               "<input type='hidden' name='MyLines[#= index(data)#].ItemCode' value='#= ItemCode #' />"
             );
             columns.Bound(p => p.Description).ClientTemplate("#= Description #" +
               "<input type='hidden' name='MyLines[#= index(data)#].Description' value='#= Description #' />"
             );
             columns.Bound(p => p.UoM).ClientTemplate("#= UoM #" +
               "<input type='hidden' name='MyLines[#= index(data)#].UoM' value='#= UoM #' />"
             );
             columns.Bound(p => p.QtyCC).ClientTemplate("#= QtyCC #" +
                "<input type='hidden' name='MyLines[#= index(data)#].QtyCC' value='#= QtyCC #' />"
             );
             columns.Bound(p => p.QtyEmployee).ClientTemplate("#= QtyEmployee #" +
                "<input type='hidden' name='MyLines[#= index(data)#].QtyEmployee' value='#= QtyEmployee #' />"
             );
             columns.Bound(p => p.ItemListLineID).Hidden(true).ClientTemplate("#= ItemListLineID #" +
                "<input type='hidden' name='MyLines[#= index(data)#].ItemListLineID' value='#= ItemListLineID #' />"
             );
             columns.Bound(p => p.ItemListCode).Hidden(true).ClientTemplate("#= ItemListCode #" +
                "<input type='hidden' name='MyLines[#= index(data)#].ItemListCode' value='#= ItemListCode #' />"
             );
             columns.Command(command =>
             {
                 command.Destroy();
             }).Width(200);
         })
    .DataSource(dataSource => dataSource.Ajax()
         .Model(m =>
         {
             m.Id(p => p.ItemCode);
             m.Field(p => p.ItemCode).Editable(false);
             m.Field(p => p.Description).Editable(false);
             m.Field(p => p.UoM).Editable(false);
             m.Field(p => p.QtyCC).Editable(true);
             m.Field(p => p.QtyEmployee).Editable(true);
             m.Field(p => p.ItemListLineID).Editable(false);
             m.Field(p => p.ItemListCode).Editable(false);
         })
         .Batch(true)
         .ServerOperation(false)
         // these are dummy action methods that don't really exist.
         .Update("upd", "upd") 
         .Destroy("del", "del")
         .Create("cre", "cre")
    )
    .Navigatable()
)

这里的“索引”功能:

function index(dataItem) {
    var data = $("#RequestLinesGrid").data("kendoGrid").dataSource.data();

    return data.indexOf(dataItem);
}


Answer 2:

因此,这里是一个解决你的问题的一部分。 我延长了剑道数据源,所以你可以调用这个函数,并得到所有的更新,删除,创建的项目出来。 只需把它变成JSON和您的表单字段的其余部分发回。 我现在已经得到了这个工作。 我以为我会分享。

kendo.data.DataSource.prototype.GetUnsavedData = function () {
    var that = this,
        idx,
        length,
        created = [],
        updated = [],
        destroyed = that._destroyed,
    allRows = [],
        data = that._flatData(that._data);

    for (idx = 0, length = data.length; idx < length; idx++) {
        if (data[idx].isNew()) {
            created.push(data[idx]);
        } else if (data[idx].dirty) {
            updated.push(data[idx]);
        }
    }

    allRows = created.concat(updated).concat(destroyed);
    var allRowsJSON = JSON.stringify(allRows);
    return allRowsJSON;
}


Answer 3:

我的灵感来自于codebeastie和@ user1878526产生了混合的想法-简单地返回电网为字符串控制器保存操作的列表:

控制器:

Function Save(ThisForum As myModel, gridData As List(Of String)) As ActionResult

视图:

$("#myForm").submit(function (event) {
    var grid = $("#myGrid").data("kendoGrid");
    var data = grid.dataSource.view();
    var input;
    for (var i = 0; i < data.length; i++) {
        var s = JSON.stringify(data[i]).replace(/["']/g, "");
        input = $("<input>", { type: 'hidden', name: 'data[' + i + ']', value: s });
        $(this).append($(input));
    }
})

然后可以在控制器通过分析合理容易Split荷兰国际集团的逗号和冒号。



文章来源: Submit form with Kendo MVC Grid and other elements