View Page:
<ej-grid id="Grid" allow-paging="true" action-failure="failure">
...
</ej-grid>
<script type="text/javascript">
function failure(args) {
alert(args.error.statusText); //get the exception message
}
</script>
Controller:
namespace SyncfusionASPNETCoreApplication2.Controllers
{
public partial class GridController : Controller
{
.....
public ActionResult NormalInsert([FromBody]CRUDModel<Employees> value)
{
if (!ModelState.IsValid) //if validation got failed
{
var message = string.Join(" | ", ModelState.Values.SelectMany(v => v.Errors).Select(e => e.ErrorMessage));
return new BadRequestObjectResult(HttpStatusCode.BadRequest);//message returns the exception content
}
emp.Insert(emp.Count, value.Value);
return Json(emp);
}
......
}
}
|
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
So you can do some server-side validation (which would be a legitimate reason for using the adapter) and utilize the ModelState.AddModelError the seems like a highly supportable feature.
View Page:
<ejs-grid id="Grid" allowPaging="true" actionFailure="failure">
...
</ejs-grid>
<script type="text/javascript">
function failure(args) {
alert(args.error.statusText); //get the exception message
}
</script>
Controller:
namespace SyncfusionASPNETCoreApplication.Controllers
{
public partial class GridController : Controller
{
.....
public ActionResult NormalInsert([FromBody]CRUDModel<Employees> value)
{
if (!ModelState.IsValid) //if validation got failed
{
var message = string.Join(" | ", ModelState.Values.SelectMany(v => v.Errors).Select(e => e.ErrorMessage));
return new BadRequestObjectResult(HttpStatusCode.BadRequest);//message returns the exception content
}
}
......
}
}
|
var message = string.Join(" | ", ModelState.Values.SelectMany(v => v.Errors).Select(e => e.ErrorMessage));
return new BadRequestObjectResult(HttpStatusCode.BadRequest);//message returns the exception content
BadRequestObjectResult badRequest = new BadRequestObjectResult(HttpStatusCode.BadRequest);
badRequest.Value = message;
return badRequest;
[Index.cshtml]
ejs-grid id="Grid" allowPaging="true" actionFailure="actionFailure" toolbar="@(new List<string>() {"Add", "Edit", "Update", "Delete" })">
<e-data-manager url="/Home/UrlDataSource" adaptor="UrlAdaptor" insertUrl="/Home/Insert" updateUrl="/Home/Update" removeUrl="/Home/Remove"></e-data-manager>
. . . .
</ejs-grid>
<script type="text/javascript">
function actionFailure(args) {
alert(args.error.statusText); //get the exception message
}
</script>
[HomeController.cs]
public IActionResult Insert([FromBody]CRUDModel<Orders> Value)
{
try
{
if (ModelState.IsValid)
{
order.Insert(0, Value.Value);
}
return Json(Value.Value);
}
catch (Exception e) {
throw new Exception(e.Message);//message returns the exception content
}
} |