使用Razor MVC 4.0
我有一个视图,其必填字段为“Name
”(在模型中指定)。
我有Kendo Grid / EditMode InLine / Server bound Data source
(见下文)
@(Html.Kendo()。网格(模型)
.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
(名称为空时为false)。
没有更新。
返回网格。
[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);
}
但是 - 验证消息显示为“未”。
如何显示验证消息?
答案 0 :(得分:-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 controller code中看到以下内容:
return Json(results.ToDataSourceResult(request, ModelState));