1

使用Razor MVC 4.0 我有一个必填字段为“ Name”的视图(在模型中指定)。我有一个Kendo Grid / EditMode InLine / Server bound Data source(见下文)

@(Html.Kendo().Grid(模型)

  .Name("Grid") 

  .Events(e => e.Edit("gridChange")) 

  .Columns(columns =>
  {
      columns.Bound(p => p.Id).Hidden();   //Create a column bound to the "ProductID" property
      columns.Bound(p => p.Name).Width(120); //Create a column bound to the "ProductName" property
      columns.Bound(p => p.SortValue).Width(80).EditorTemplateName("SortNumericTextBox");   //Create a column bound to the "UnitPrice" property
      columns.Bound(p => p.Active).Width(100);//Create a column bound to the "UnitsInStock" property

      columns.Command(command => command.Edit()).Width(100);
  })
 .ToolBar(toolbar => toolbar.Create())
 .Editable(editable => editable.Mode(GridEditMode.InLine))
 .DataSource(dataSource => dataSource
        .Server()
        .Model(model =>
        {
            model.Id(p => p.Id);
            model.Field(p => p.Name ).Editable(true);
            model.Field(p => p.SortValue);
            model.Field(p => p.Active);

        })

     // Configure CRUD -->
        .Create(create => create.Action("Create", "MonitorType"))
        .Read(read => read.Action("Index", "MonitorType"))
        .Update(update => update.Action("Edit", "MonitorType"))         
        .PageSize(5)

       )
 .Pageable() //Enable paging

 )

在控制器(HTTP)中编辑和创建检查ModelState.IsValid(名称为空时为假)。没有更新发生。返回网格。

    [HttpPost]
    public ActionResult Create(MonitorType monitortype)
    {
        if (ModelState.IsValid)
        {
            unitOfWork.MonitorTypeRepository.Insert(monitortype);
            unitOfWork.Save();
            return RedirectToAction("Index");
        }

        //GridRouteValues() is an extension method which returns the
        //route values defining the grid state - current page, sort expression, filter etc.
        RouteValueDictionary routeValues = this.GridRouteValues();
        return RedirectToAction("Index", routeValues);
    }

但是 - 验证消息是“不”显示。

您如何显示验证消息?

4

1 回答 1

-1

据我所知,需要两件事。

DataSource首先,在(例如.ajax().events(events => events.Error("error_handler")))中分配错误处理程序事件。

其次,添加错误处理程序脚本(此代码几乎在每个 Kendo UI 演示中都可用):

 function error_handler(e, status) {
    if (e.errors) {
        var message = "The following are errors:\n";
        $.each(e.errors, function (key, value) {
            if ('errors' in value) {
                $.each(value.errors, function () {
                    message += this + "\n";
                });
            }
        });
        alert(message);
    }
}

最后,控制器需要返回ModelState作为参数,以便显示错误。再次查看网格演示,您将在MVC 控制器代码中看到以下内容:

return Json(results.ToDataSourceResult(request, ModelState));
于 2013-04-03T21:43:54.443 回答