- Home
- Forum
- ASP.NET MVC - EJ 2
- How to return Custom message from Controller when CRUD action fails
How to return Custom message from Controller when CRUD action fails
The code below fires correctly on the grid ActionFailure but the alert message show "Undefined".
....
@Html.EJS().Grid("Grid").DataSource(ds => { ds.Url("/Cargos/UrlDatasource").InsertUrl("/Cargos/Insert").UpdateUrl("/Cargos/Update").RemoveUrl("/Cargos/Delete").Adaptor("UrlAdaptor"); }).ToolbarClick("toolbarClick").ActionComplete("actionComplete").RowSelected("rowSelected").Columns(col =>
{
col.Field("Id").HeaderText("ID").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).IsPrimaryKey(true).Visible(false).Add();
col.Field("codigo").HeaderText("Código").Width("20").Add();
col.Field("nombre").HeaderText("Nombre").Width("80").Add();
col.Field("basico").HeaderText("Sueldo Básico").Width("40").EditType("numericedit").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
col.Field("sede").HeaderText("Sede").Width("20").Add();
}).DataBound("dataBound").ActionFailure("actionFailure").AllowPaging(true).EditSettings(edit => { edit.AllowAdding(true).AllowEditing(true).AllowDeleting(true).ShowDeleteConfirmDialog(true).Mode(Syncfusion.EJ2.Grids.EditMode.Dialog).Template("#dialogtemplate"); }).Toolbar(toolbarItems).Locale("es").Render()
{
col.Field("Id").HeaderText("ID").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).IsPrimaryKey(true).Visible(false).Add();
col.Field("codigo").HeaderText("Código").Width("20").Add();
col.Field("nombre").HeaderText("Nombre").Width("80").Add();
col.Field("basico").HeaderText("Sueldo Básico").Width("40").EditType("numericedit").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
col.Field("sede").HeaderText("Sede").Width("20").Add();
}).DataBound("dataBound").ActionFailure("actionFailure").AllowPaging(true).EditSettings(edit => { edit.AllowAdding(true).AllowEditing(true).AllowDeleting(true).ShowDeleteConfirmDialog(true).Mode(Syncfusion.EJ2.Grids.EditMode.Dialog).Template("#dialogtemplate"); }).Toolbar(toolbarItems).Locale("es").Render()
...
public ActionResult Insert(CargoSet value)
{
var obj = db.CargoSet.Where(o => o.codigo == value.codigo).SingleOrDefault();
if (obj != null) //Check already exisiting
{
throw new InvalidOperationException("Cargo ya existe para esa Sede");//Exception thrown if exisiting
}
{
var obj = db.CargoSet.Where(o => o.codigo == value.codigo).SingleOrDefault();
if (obj != null) //Check already exisiting
{
throw new InvalidOperationException("Cargo ya existe para esa Sede");//Exception thrown if exisiting
}
....
function actionFailure(args) {
alert(args.error.statusText); //get the exception message });
Please help. I'm using EJ2
SIGN IN To post a reply.
5 Replies
MS
Madhu Sudhanan P
Syncfusion Team
April 2, 2019 07:21 AM UTC
Hi Luis,
Thanks for contacting Syncfusion support.
During AJAX requests simply throwing exception at the server side will not return the formatted error details. Please refer to the below help links to write statuscode and error details at the server side.
When the error is returned as specified in the above links, you can access them at the actionFailure event properly.
Regards,
Madhu Sudhanan P
LU
luis
April 2, 2019 11:58 AM UTC
PS
Pavithra Subramaniyam
Syncfusion Team
April 3, 2019 07:28 AM UTC
Hi Luis,
Thanks for your update.
We have analyzed your query and found that you have not properly extracted the custom message in ActionFailure event. Please refer the following code snippet to achieve your requirement.
|
[Client side code]
@Html.EJS().Grid("Grid").DataSource(ds => ds.Url("/Home/UrlDatasource").Adaptor("UrlAdaptor").InsertUrl("/Home/Insert")
.RemoveUrl("/Home/Remove").UpdateUrl("Home/Update")).AllowPaging(true).Columns(col =>
{
col.Field("OrderID").HeaderText("Order ID").IsPrimaryKey(true).TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Width("120").Add();
...
}).Toolbar(new List<string>() { "Add", "Edit", "Delete", "Update", "Cancel" }).ActionFailure("failure").EditSettings(edit => { edit.AllowAdding(true).AllowDeleting(true).AllowEditing(true); }).Render()
<script>
function failure(args) {
var errorMessage = args.error[0].error.responseText.split("Exception:")[1].split('<br>')[0]; //extract the message from args
alert(errorMessage);
}
</script>
[Server side code]
public ActionResult Insert(Orders value)
{
var data = order.Where(or => or.OrderID == value.OrderID).FirstOrDefault();
if (data == null)
{
order.Insert(0, value);
}
else
{
throw new Exception("Duplicate values cannot be inserted"); //Add custom exception message
}
return Json(value);
} |
We have attached a sample for your reference. Please find the sample below:
Sample link: http://www.syncfusion.com/downloads/support/directtrac/general/ze/EJ2Grid_-_F_143686-816417296.zip
Regards,
Pavithra S.
LU
luis
April 3, 2019 03:27 PM UTC
Worked, Perfect...
Thanks.
HJ
Hariharan J V
Syncfusion Team
April 4, 2019 05:07 AM UTC
Hi Luis,
Thanks for your update.
We are happy to hear that the provided solution helped you.
Please contact us if you need any further assistance. As always, we will be happy to assist you.
Regards,
Hariharan
Thanks for your update.
We are happy to hear that the provided solution helped you.
Please contact us if you need any further assistance. As always, we will be happy to assist you.
Regards,
Hariharan
SIGN IN To post a reply.
- 5 Replies
- 4 Participants
-
LU luis
- Apr 1, 2019 08:52 AM UTC
- Apr 4, 2019 05:07 AM UTC