Hi,
this is javascript code:
function gridFailure(args) {
var str = "";
var message = "";
var dialogError = document.getElementById('ErrorList').ej2_instances[0]
str = args.error[0].error.responseText;
message = "<p>" + str + "</p>";
dialogError.content = message;
dialogError.show();
};
This is controller:
public async Task<ActionResult> InsTipiCarrello(TipoCarrello value, string action)
{
log4net.Config.XmlConfigurator.Configure();
log.Info("Inizio esecuzione InsTipiCarrello(TipoCarello value, string action)");
try
{
TipoCarrello tipoCarello = _tipoCarrelloRepository.CreateTipoCarrello();
using (_tipoCarrelloRepository)
{
ModelState.Remove("value.IdTipoCarrello");
value.IdTipoCarrello = Guid.NewGuid();
if (ModelState.IsValid)
{
tipoCarello.IdTipoCarrello = value.IdTipoCarrello;
tipoCarello.Tipo = value.Tipo;
tipoCarello.Codice = value.Codice;
if (await _tipoCarrelloRepository.ExistTipoCarrello(value.Tipo))
{
Response.StatusCode = 412;
throw new Exception(Server.HtmlEncode(Resources.TipiCarrello.TipoGiaPresente));
}
else
{
if (await _tipoCarrelloRepository.InsTipoCarrello(tipoCarello) != 1)
{
Response.StatusCode = 412;
throw new Exception(Resources.Common.ErroreTrasmissione);
}
}
}
else
{
var message = string.Join(" | ", ModelState.Values.SelectMany(v => v.Errors).Select(e => e.ErrorMessage));
Response.StatusCode = 412;
throw new Exception(Server.HtmlEncode(Server.HtmlEncode(message)));
}
log.Info("Fine esecuzione InsTipiCarrello(TipoCarello value, string action)");
}
}
catch (Exception ex)
{
var message = ex.InnerException == null ? ex.Message : ex.InnerException.Message;
log.Error(string.Format("Errore:{0}", message.ToString()));
Response.StatusCode = 412;
throw new Exception(Server.HtmlEncode(Server.HtmlEncode(message)));
}
return Json(value);
}
But args.error not return custom exception message selected, and in insert if i throw exception grid refresh but is empty.
public ActionResult Insert([FromBody]CRUDModel<OrdersDetails> requestData)
{
if (requestData.value.CustomerID == null)
{
throw new Exception("Cannot insert row - 'CustomerID' is empty");
}
else
{
OrdersDetails.GetAllRecords().Insert(0, requestData.value);
}
return Json(requestData.value);
} |
Hi Sujith
thank you fo rsupport.
as you could see in the code I shared with you, I use your own code but args doesn't return the desired value, but another custom error that in debug is never injected into the grid, so I don't understand where args takes it from.
I commented on all the code that refers to the error returned but arg keeps give me the same error.
Hi Sujith,
with the code I showed you above, I get the following result:
- This is the message I expect:
-This is the message that returns args:
if (requestData.value.CustomerID == null)
{
var message = "Cannot insert row - 'CustomerID' is empty";
Response.StatusCode = 412;
throw new Exception(Server.HtmlEncode(Server.HtmlEncode(message)));
} |
Hi Sujith,
I found that the problem is due to enabling the custom erro page in the webconfig.
If I disable those settings in the web.config, I get the error as shown, but as soon as I reactivate, the Resposne.StatusCode= 412 which is hardcoded, generates the message I indicated overwriting what I pass via args.
If I delete the 412 it generates me a 500 error.
How can I fix it?
Hi Sujith,
I modified your example, to show you how the problem occurs.
I added a controller for handling error pages, error pages and the web.config
public static class Globals
{
public static string CustomMessage { get; set; }
}
public ActionResult Insert([FromBody]CRUDModel<OrdersDetails> requestData)
{
if (requestData.value.CustomerID == null)
{
Response.StatusCode = 412;
Globals.CustomMessage = "/Cannot insert row CustomerID is empty?";
throw new Exception();
}
...
return Json(requestData.value);
} |
public ViewResult ServerError()
{
try
{
Response.StatusCode = 500;
string errorMsg = Globals.CustomMessage;
Response.Write(errorMsg);
Response.TrySkipIisCustomErrors = true;
return View("500");
}
...
} |
// Grid’s actionFailure event handler
function onActionFailure(args) {
var errorDetails = args.error[0].error.responseText;
// Custom error message is retrieved
var errorText = errorDetails.substring(errorDetails.indexOf('/') + 1, errorDetails.indexOf('?'));
} |
Hi Sujith,
Perfect!!
Thank you for support.