Asp.net mvc Syncfusion Asp.Net Datagrid:如何从CRUD类返回错误
我有一个Syncfusion Asp.net网格执行CRUD操作Asp.net mvc Syncfusion Asp.Net Datagrid:如何从CRUD类返回错误,asp.net-mvc,syncfusion,Asp.net Mvc,Syncfusion,我有一个Syncfusion Asp.net网格执行CRUD操作 public ActionResult Insert(CRUDModel<Source> newItem) { using (var context = new ImageStormEntities()) { context.Sources.Add(newItem.Value); context.SaveChanges(); } return Json(
public ActionResult Insert(CRUDModel<Source> newItem)
{
using (var context = new ImageStormEntities())
{
context.Sources.Add(newItem.Value);
context.SaveChanges();
}
return Json(newItem.Value);
}
public ActionResult插入(CRUDModel newItem)
{
使用(var context=newImageStormEntities())
{
context.Sources.Add(newItem.Value);
SaveChanges();
}
返回Json(newItem.Value);
}
cshtml:
@Html.EJS().Grid("DataGrid").DataSource(ds => ds.Json(ViewBag.datasource).UpdateUrl("/Management/Update").InsertUrl("/Management/Insert").RemoveUrl("/Management/Remove").Adaptor("RemoteSaveAdaptor")).Columns(col =>
{
col.Field("id").IsPrimaryKey(true).Visible(false).Add();
col.Field("ResourceGroup").HeaderText("Source VM Resource Group").Add();
col.Field("VMName").HeaderText("Source VM Name").Add();
col.Field("imageVersion").HeaderText("Image Version").Add();
}).ActionFailure("OnActionFailure").AllowTextWrap(true).TextWrapSettings(text => { text.WrapMode(Syncfusion.EJ2.Grids.WrapMode.Header); }).AllowPaging().FilterSettings(filter => { filter.Type(Syncfusion.EJ2.Grids.FilterType.Menu); }).EditSettings(edit => { edit.AllowAdding(true).AllowEditing(true).AllowDeleting(true).ShowDeleteConfirmDialog(true).Mode(Syncfusion.EJ2.Grids.EditMode.Dialog); }).Toolbar(new List<string>
() { "Add", "Edit", "Delete", "Update", "Cancel" }).Render()
@Html.EJS()
{
col.Field(“id”).IsPrimaryKey(true).可见(false).添加();
col.Field(“ResourceGroup”).HeaderText(“源VM资源组”).Add();
col.Field(“VMName”).HeaderText(“源VM名称”).Add();
col.Field(“图像版本”).HeaderText(“图像版本”).Add();
}).ActionFailure(“OnActionFailure”).AllowTextWrap(true).TextWrapSettings(text=>{text.WrapMode(Syncfusion.EJ2.Grids.WrapMode.Header);}).AllowPaging().FilterSettings(filter=>{filter.Type(Syncfusion.EJ2.Grids.FilterType.Menu);}).EditSettings(edit=>{edit.AllowAddressing(true).AllowEditing(true).AllowDeleteEleting(true).ShowDeleteConfirmDialog(true).Mode(Syncfusion.EJ2.Grids.EditMode.Dialog);}).Toolbar(新列表
(){“添加”、“编辑”、“删除”、“更新”、“取消”})
在页面上,我有:
<script>
function OnActionFailure(args) {
alert(args.error.status + " : " + args.error.statusText);
}
</script>
函数OnActionFailure(args){
警报(args.error.status+”:“+args.error.statusText);
}
当值打破db约束,但值为空时,返回一个简单的toast消息。
我想向用户发送有用的信息
我可以捕捉到错误,我要返回什么才能使它工作。
此外,我更希望使用Messagebox来释放,而不是祝酒。更改了控制器代码,这将解决您的问题 到
public ActionResult插入(CRUDModel newItem)
{
使用(var context=newImageStormEntities())
{
if(newItem.Value!=null)
{
context.Sources.Add(newItem.Value);
SaveChanges();
}
其他的
{
抛出新异常(“无法插入空值”);//添加自定义
异常消息
}
}
返回Json(newItem.Value);
}
在你的js中加上石灰
<script>
function OnActionFailure(args) {
var errorMessage = args[0].error.responseText.split("Exception:")[1].split('<br>')[0]; //extract the message from args
alert(errorMessage);
//you will get exact error that you have returned from serverside in errorMessage
}
</script>
函数OnActionFailure(args){
var errorMessage=args[0]。error.responseText.split(“异常:”)[1]。split(“
”)[0];//从args提取消息
警报(错误消息);
//您将在errorMessage中获得从服务器端返回的确切错误
}
@jlo gmail..这对您有帮助吗?它只在以下情况下起作用:ASPNETCORE\u环境设置为“开发”!使用此设置部署应用程序并不好。
<script>
function OnActionFailure(args) {
var errorMessage = args[0].error.responseText.split("Exception:")[1].split('<br>')[0]; //extract the message from args
alert(errorMessage);
//you will get exact error that you have returned from serverside in errorMessage
}
</script>