简体   繁体   English

尝试添加或编辑项目时,Kendo UI ASP.NET MVC ListView失败

[英]Kendo UI ASP.NET MVC ListView fails when trying to add or edit items

I'm struggling to set up a ListView which allows me to add new records and to update existing ones. 我正在努力建立一个ListView,它使我可以添加新记录并更新现有记录。 Loading the items for the list works like a charm. 加载列表中的项目就像一个超级按钮。 Also showing the only relevant information Id and Line works perfectly. 同时显示IdLine的唯一相关信息也很完美。

Two problems: 两个问题:
1. Adding a new item to the list: I see the ugly form which allows to set the value for Id and Line . 1.在列表中添加一个新项目:我看到了一个丑陋的表格,该表格允许设置IdLine的值。 But there is no way for me to save it. 但是我没有办法保存它。
2. Updating an existing item: I see the ugly form which allows to set the value for Id and Line . 2.更新现有项目:我看到了丑陋的表格,该表格允许设置IdLine的值。 But the fields are empty. 但是这些字段是空的。 They should be pre-filled with whatever has been shown before. 他们应该预先填充之前显示的内容。 Again, there is no way for me to save it. 同样,我没有办法保存它。

My code looks pretty similar to what is provided in Telerik's Examples Project. 我的代码看起来与Telerik的示例项目中提供的代码非常相似。 Unfortunately, I fail to find the minor difference... 不幸的是,我找不到细微的差别...

The extremely stripped code of the ListView (still not working as intended): ListView的极其剥离的代码(仍然无法按预期工作):

@using System.Collections
@using TestcaseRepositoryAPI.Model.Domain;

@model TemplateGeneratorItem

@{
    ViewBag.Title = "VORLAGE";
    ViewBag.SubTitle = "erstellen";
    ViewBag.ShowMenu = true;
    Layout = "~/Views/Shared/_LayoutMetro.cshtml";
}

@Html.Partial("_MetroPageHeader")

<div class="demo-section">
    <a class="k-button k-button-icontext k-add-button" href="#"><span class="k-icon k-add"></span>Add new record</a>
</div>

<div class="demo-section k-header">
    @(Html.Kendo().ListView<TemplateGeneratorItem.Record>(Model.Records)
        .Name("RecordsList")
        .ClientTemplateId("recordLinesTemplate")
        .DataSource(d => d
            .Events(e => e.RequestStart("requestStart").RequestEnd("requestEnd"))
            .Create("RecordLineCreate", "Metro")
            .Read("RecordLinesRead", "Metro")
            .Update("RecordLineUpdate", "Metro")
            .Destroy("RecordLineDelete", "Metro")
            .Model(m => m.Id(o => o.Line))
        )
        .Editable()
        //.Events(e => e.Remove("removeRecord"))
        .HtmlAttributes(new { style = "border:none;" })
        .TagName("div")
    )
</div>

<script type="text/x-kendo-tmpl" id="recordLinesTemplate">
    <div class="k-widget" style="margin:10px auto 10px auto;">
        <fieldset>
            <legend>Zeile #=Line#</legend>

            <div class="edit-buttons">
                <a class="k-button k-button-icontext k-edit-button" href="\\#"><span class="k-icon k-edit"></span></a>
                <a class="k-button k-button-icontext k-delete-button" href="\\#"><span class="k-icon k-delete"></span></a>
            </div>

            <dl>
                <dt>ID</dt>
                <dd>#:Id#</dd>
            </dl>
        </fieldset>
    </div>
</script>

<script type="text/javascript">
    function requestStart(e) {
        console.log("requestStart(e)", e);
    }

    function requestEnd(e) {
        console.log("requestEnd(e)", e);
        console.log("e.type", e.type);
        if (e.response) {
            console.log("e.response", e.response);
        }
    }

    function getRecList() {
        return $("#RecordsList").data("kendoListView");
    }

    $(".k-add-button").click(function (e) {
        getRecList().add();
        e.preventDefault();
    });
</script>

The snippet of the controller which returns the data: 返回数据的控制器的代码段:

public JsonResult RecordLinesRead([DataSourceRequest] DataSourceRequest request)
{
    List<TemplateGeneratorItem.Record> records = GetTemplateGeneratorItemFromSession().Records;
    int line = 1;
    foreach (TemplateGeneratorItem.Record record in records)
    {
        record.Line = line++;
    }

    return Json(records.ToTreeDataSourceResult(request), JsonRequestBehavior.AllowGet);
}

And finally the classes of the model: 最后是模型的类:

namespace TestcaseRepositoryAPI.Model.Domain
{
    [DataContract]
    public class TemplateGeneratorItem
    {
        [DataMember]
        public List<Record> Records { get; set; }

        public class Record
        {
            [DataMember]
            //[ScaffoldColumn(false)]
            public int Line { get; set; }

            [DataMember]
            public string Id { get; set; }

            [DataMember]
            [ScaffoldColumn(false)]
            public List<RecordField> Fields { get; set; }

            public Record() { }

            public Record(Record record)
            {
                Line = record.Line;
                Id = record.Id;
                Fields = record.Fields;
            }
        }

        public class RecordField
        {
            [DataMember]
            public string Name { get; set; }

            ...
        }
    }
}

Found it. 找到了。 The Telerik support asked the right questions. Telerik支持人员提出了正确的问题。

The problem is quite simple: There is no editor template specified! 问题很简单:没有指定编辑器模板! There are (at least?) two ways to resolve this issue: 有(至少吗?)两种方法可以解决此问题:
1. Specify the name of the editor template (eg .Editable(e => e.Editable(true).TemplateName("TemplateGeneratorRecord")) ) and create the TemplateGeneratorRecord.cshtml in the EditorTemplates folder. 1.指定编辑器模板的名称(例如.Editable(e => e.Editable(true).TemplateName("TemplateGeneratorRecord")) ),然后在EditorTemplates文件夹中创建TemplateGeneratorRecord.cshtml。
2. Or let some magic happen by creating an editor template in the EditorTemplates folder called Record.cshtml. 2.或者通过在EditorTemplates文件夹中创建一个名为Record.cshtml的编辑器模板来发生一些魔术。 That's the solution used in Telerik's examples. 这就是Telerik的示例中使用的解决方案。

I've chosen the first option. 我选择了第一个选项。 Now it is working as expected. 现在它正在按预期工作。

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

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