发生错误时,为什么我的数据源中的错误事件没有触发?

时间:2019-05-15 13:56:07

标签: javascript asp.net-mvc kendo-ui kendo-asp.net-mvc donut-chart

我试图让我的DonutChart在ModelState中出现错误时显示错误。但是,不会触发DataSource.Events.Error中的事件。我在这里想念东西吗?

我有一个包含图表的cshtml文件:

@{ string shortDept = "OW"; }

@model IEnumerable<DonutValue>
@(Html.Kendo().Chart<DonutValue>()
    .Name("Donut")
    .DataSource(dataSource => dataSource
        .Read(read => read
            .Action("Get_KPI", "Data", new { dept = shortDept })
        )
        .Events(events => events
            .Error("checkError")
        )
    )
    .Series(series => { series
        .Donut(
            model => model.Value,
            model => model.Description,
            model => model.Color,
            null
        )
        .HoleSize(50)
        .Size(16)
        .StartAngle(90);
    })
    .Events(events => events
        .SeriesHover("onDonutSeriesHover")
        .Render("onDonutRender")
    )
)

模型如下:

public class DonutValue
{
    public string Department { get; set; }
    public decimal Value { get; set; }
    public string Color { get; set; }
    public string Description { get; set; }
}

Controller工作并返回数据,但ModelState错误:

public class DataController : Controller
{
    [HttpPost]
    public JsonResult Get_KPI([DataSourceRequest] DataSourceRequest request, string dept)
    {
        ModelState.AddModelError("Error", "Testfoutmelding!");
        KPIServicesClient client = new KPIServicesClient();
        List<DonutChartValue> values = client.GetDonutChartValues() ?? new List<DonutChartValue>();
        var data = values.FirstOrDefault(x => x.Department == dept);
        var data2 = new DonutValue[] {
            new DonutValue() {Value = data.CurrentSLA, Color = data.SLAColor, Description = $"Huidige SLA: {data.SLAShowValue}", Department = dept},
            new DonutValue() {Value = data.PotentialPlusSLA, Color = data.PotentialSLAColor, Description = $"Potentiële SLA: {data.PotentialSLAShowValue}", Department = dept },
            new DonutValue() {Value = data.LostSLA, Color = "#EFEFEF", Description = "", Department = dept}
        };

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

JavaScript函数仅显示出于测试目的的警报:

<script>
  function checkError(e) {
    alert("Error!");
  }
</script>

我验证了错误实际上是在JSON结果中返回的: https://imgur.com/AFacf99

用于处理错误的JavaScript代码与用于处理Render和SeriesHover的代码位于同一文件中,并且它们被触发,因此可以访问该脚本。

我在这里错过了什么吗?请帮忙!

谢谢!

1 个答案:

答案 0 :(得分:0)

响应对象中包含错误的字段应在DataSource定义的模式中明确定义:

#Find all versions available:
python2.7 -m pip install scikit-image==
#Then install your prefer/latest version from the list above. Example:
python2.7 -m pip install scikit-image==0.14.2

否则,默认字段称为"errors"(小写的“ e”),但是响应字段实际上是“错误”(大写的“ E”)。

有关自定义数据源的更多信息,请参见official docs