MVCWebAPIpost方法中如何将ModelState错误返回给Kendo网格?我找不到Kendo+MVCWebAPI的示例,其中发布/更新方法返回验证错误。看起来没有Kendo扩展来使下面的代码工作。publicHttpResponseMessagePost([ModelBinder(typeof(Prototype.WebApi.ModelBinders.DataSourceRequestModelBinder))][DataSourceRequest]DataSourceRequest请求,Useruser){if(this.ModelState.IsValid){//save}returnRequest.CreateErrorResponse(HttpRequest.Code,这个.ModelState.ToDataSourceResult());因此,在此上下文中的ModelState是System.Web.Http.ModelBinding.ModelStateDictionary,而Kendo扩展需要System.Web.Mvc.ModelStateDictionary。那么从WebAPI向Kendo返回ModelState错误的最佳方式是什么?这对我们有用,尽管我们从未看到ModelState错误并且通常会忽略该部分......KendoGrid@modelSysMaintViewModel@(Html.Kendo().Grid().Name("BuildingsGrid").Columns(columns=>[省略的东西].DataSource(dataSource=>dataSource.Ajax()>>>.Events(e=>e.Error("error_handler")).Model(model=>{model.Id(m=>m.Id);model.Field(m=>m.ProjectId).DefaultValue(Model.ProjectId);model.Field(m=>m.IsActive).DefaultValue(true);}).Create(create=>create.Action("CreateBuilding","SysMaint")).Read(read=>read.Action("ReadBuildings","SysMaint",Model)).Update(update=>update.Action("UpdateBuilding","SysMaint")If(ModelState.IsValid){try{building=_presentationService.UpdateBuilding(modelIn);}catch(异常e){ModelState.AddModelError(string.Empty,e.Message);}}else{varerrMsg=ModelState.Values.Where(x=>x.Errors.Count>=1).Aggregate("模型状态错误:",(current,err)=>current+err.Errors.选择(x=>x.ErrorMessage));ModelState.AddModelError(string.Empty,errMsg);}varbuildings=(newList{building}).ToDataSourceResult(request,ModelState);返回Json(建筑物,JsonRequestBehavior.AllowGet);更新的控制器我们发现这个流程工作得更好,它向Elmah添加了错误日志记录(通用示例)...[HttpPost]publicJsonResultUpdate([DataSourceRequest]DataSourceRequestrequest,MyObjectModelmodelIn){myObject=_presentationService.Update(modelIn,User.Identity.Name);varmyObjectList=newList{myObject};返回Json(myObjectList.ToDataSourceResult(request,ModelState),JsonRequestBehavior.AllowGet);}else{varmyObjectList=newList{modelIn};返回Json(myObjectList.ToDataSourceResult(请求,ModelState),JsonRequestBehavior.AllowGet);}}catch(Exceptione){Elmah.ErrorSignal.FromCurrentContext().Raise(e);ModelState.AddModelError(string.Empty,e.Message);varmyObjectList=newList{modelIn};返回Json(myObjectList.ToDataSourceResult(request,ModelState),JsonRequestBehavior.AllowGet);}}常见的JavaScript和Kendo窗口@(Html.Kendo().Window().Name("alertWindow").Title("来自服务器的状态消息").Draggable().Resizable().Width(400).Height(200).Modal(true).Visible(false))functionshowAlertWindow(message){varalertWindow=$('#alertWindow').dataalertWindow.content(message);alertWindow.refresh();alertWindow.center();alertWindow.open();}functionerror_handler(e){if(e.errors){varmessage="Errors:n";$.each(e.errors,function(key,value){if('errors'invalue){$.each(value.errors,function(){message+=this+"n";});}});showAlertWindow(消息);额外的是,我们的BaseModel也有一个ErrorMessage参数,如果它应该是其他任何东西的话打开相同的警报窗口,我们将把其他类型的错误放入页面加载的检查中$(document).ready(function(){if("@Model.ErrorMessage"!=""){showAlertWindow("@Model。错误信息”);}});当发生错误时,这会提供非常好的演示-所以我们的内部用户不会惊慌。我希望这可以帮助你。以上就是C#学习教程:如何在MVCWebAPIpost方法中将ModelState错误返回给Kendo网格?如果所有分享的内容对你有用,需要进一步了解C#学习教程,希望大家多多关注。本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处:
