KendoUI(Telerik 2018)-TreeList在手动刷新之前不会显示新创建的记录

时间:2019-02-04 08:24:08

标签: kendo-ui kendo-asp.net-mvc telerik-mvc kendo-treelist

我能够在TreeList中为MVC创建新记录。但是它仅在刷新页面后显示新创建的记录。编辑和删除工作正常。

剃刀代码

@(Html.Kendo().TreeList<KendoEx.Web.Models.Level>()
.Name("lvlTreeList")
.Toolbar(toolbar =>
{
    toolbar.Create();
})
.Columns(columns =>
{
    columns.Add().Field(e => e.LvlName).Title("Name").Width(120);
    columns.Add().Field(e => e.LvlType).Title("Type").Width(80);
    columns.Add().Field(e => e.Rating1).Title("Rating 1").Filterable(false).Width(100);
    columns.Add().Field(e => e.Rating2).Title("Rating 2").Filterable(false).Width(100);
    columns.Add().Command(c =>
    {
        c.CreateChild().Text("Add child");
        c.Edit();
        c.Destroy();
    }).Width(180);
})
.Editable(e => e.Mode("inline"))
.DataSource(dataSource => dataSource
    .Read(read => read.Action("GetLevel", "Level"))
    .Create(create => create.Action("Save", "Level"))
    .Update(update => update.Action("Save", "Level"))
    .Destroy(delete => delete.Action("Remove", "Level"))
    .Model(m =>
    {
        m.Id(f => f.Id);
        m.ParentId(f => f.ParentLvl).DefaultValue(0);
        m.Expanded(true);
        m.Field(f => f.LvlName);
        m.Field(f => f.LvlType);
        m.Field(f => f.Rating1);
        m.Field(f => f.Rating1);
    }))
.Height(550))

控制器方法

public JsonResult GetLevel([DataSourceRequest] DataSourceRequest request)
{
    var lvlList = LoadLevels().ToTreeDataSourceResult(request);
    return Json(lvlList, JsonRequestBehavior.AllowGet);
}

private IEnumerable<Level> LoadLevels()
{
    IEnumerable<Level> lvlList = Enumerable.Empty<Level>(); ;
    using (var client = new HttpClient())
    {
        client.BaseAddress = new Uri(ConfigurationManager.AppSettings["WebApiExUrl"]);
        var responseTask = client.GetAsync("getlevel");
        responseTask.Wait();
        var result = responseTask.Result;
        if (result.IsSuccessStatusCode)
        {
            var readTask = result.Content.ReadAsAsync<FormattedResult<Level>>();
            readTask.Wait();
            if (string.IsNullOrEmpty(readTask.Result.Error))
            {
                IEnumerable<Level> tempList = readTask.Result.Data;
                lvlList = tempList.Select(m => new Level
                {
                    Id = m.Id,
                    LvlName = m.LvlName,
                    LvlType = m.LvlType,
                    ParentLvl = m.ParentLvl,
                    Rating1 = m.Rating1,
                    Rating2 = m.Rating2,
                    hasChildren = tempList.Where(s => s.ParentLvl == m.Id).Count() > 0
                });
            }
        }
    }
    return lvlList;
}

public JsonResult Save([DataSourceRequest] DataSourceRequest request, Level level)
{
    if (level != null && ModelState.IsValid)
    {
        try
        {
            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri(ConfigurationManager.AppSettings["KpiApiUrl"]);
                var postTask = client.PostAsJsonAsync("savelevel", level);
                postTask.Wait();
                var result = postTask.Result;
                if (result.IsSuccessStatusCode)
                {
                    var readTask = result.Content.ReadAsStringAsync();
                    readTask.Wait();
                }
            }
        }
        catch (Exception ex)
        {
            ModelState.AddModelError(string.Empty, "Unexpected error");
        }
    }
    return  Json(new[] { level }.ToTreeDataSourceResult(request, ModelState));
}

型号

public class Level
{
    public int Id { get; set; }        
    public string LvlName { get; set; }
    public string LvlType { get; set; }
    public int? ParentLvl { get; set; }
    [RegularExpression(@"^(\d{1,2}(\.\d{0,2})?)?(<|<=|>|>=)(\d{1,3}(\.\d{0,2})?)$", ErrorMessage = "Invalid")]
    public string Rating1 { get; set; }
    [RegularExpression(@"^(\d{1,2}(\.\d{0,2})?)?(<|<=|>|>=)(\d{1,3}(\.\d{0,2})?)$")]
    public string Rating2 { get; set; }
}

如果“保存”方法返回

return Json(LoadLevels().ToDataSourceResult(request, ModelState), JsonRequestBehavior.AllowGet);

代替

return  Json(new[] { level }.ToTreeDataSourceResult(request, ModelState));

TreeList显示重复记录

TreeList的“保存”事件也具有相同的行为。

function onSave() {
    $("#lvlTreeList").data("kendoTreeList").dataSource.read();
}

Version Info: Telerik 2018.1.221.545, MVC 5.2, .NET 4.6

请对此提供建议。谢谢。

演示链接:https://demos.telerik.com/aspnet-mvc/treelist/editing

1 个答案:

答案 0 :(得分:0)

在DataSource的Sync事件中调用read(),刷新TreeList并显示新创建的级别。

剃刀代码

@(Html.Kendo().TreeList<..>()
.Name("lvlTreeList")
 /*..*/     
.DataSource(dataSource => dataSource
    .Events(e => e.Sync("lvlTreeList_Sync"))
    /*...*/
))

JavaScript

<script>
    function lvlTreeList_Sync() {
        this.read();
    }
</script>

引用:https://www.telerik.com/forums/refresh-after-upda