kendo grid create method return value in MVC C#

1 Answer 65 Views
Grid
abdul
Top achievements
Rank 2
Iron
abdul asked on 25 Nov 2024, 09:51 AM

I have a Kendo grid and in the Create method, I want to do some server side validation and the response I want to show to the user if validation is true or false. 

In the below code CreateTLPCurveAllocations is an MVC action method which is having return type as void.

How to use the CreateTLPCurveAllocations  method with return type, so that I can show that return value/ message to the user.

@(Html.Kendo().Grid<TLPAllocationDetails>()

    .Name("TLPCurveAllocationsGrid")
    .DataSource(datasource => datasource.Ajax().Read(read => read.Action("GetTLPCurveAllocations", "DataMgmt")))
        .Columns(columns =>
        {
        columns.Bound(p => p.AllocationDate).Editable("EditFieldsForNewRec").Format("{0:MM/dd/yyyy}").Title("Allocation Date").Width(90).HeaderHtmlAttributes(new { style = "justify-content:center" }).HtmlAttributes(new { style = "text-align: right" });
        columns.Bound(p => p.CurveDetails).Editable("EditFieldsForNewRec").ClientTemplate("#=getCurveName(data)#").Title("Curve").Width(100).HeaderHtmlAttributes(new { style = "justify-content:center" }).HtmlAttributes(new { style = "text-align: right" });
        columns.Bound(p => p.Allocation).Editable("EditAllocation").Title("Allocation").Width(100).HeaderHtmlAttributes(new { style = "justify-content:center" }).HtmlAttributes(new { style = "text-align:right" });
        })
    .ToolBar(toolbar =>
    {
        toolbar.Create();
        toolbar.Save();                    
    })
    .HtmlAttributes(new { style = "height: 400px;width:97%" })
    .Editable(editable => editable.Mode(GridEditMode.InCell))
    .Pageable()
    .Navigatable()
    .Filterable()
    .Scrollable()
    .DataSource(dataSource => dataSource
    .Ajax()
    .Batch(true)
    .PageSize(5)
    .ServerOperation(false)
    .Model(model =>
    {
        model.Id(p => p.AllocationDate);
        model.Field(p => p.CurveDetails).DefaultValue(ViewData["CurveDetails"] as CurveDetails);
    })
    .Create("CreateTLPCurveAllocations", "DataMgmt")
    .Update("UpdateTLPCurveAllocations", "DataMgmt")
    )
    )

1 Answer, 1 is accepted

Sort by
0
Accepted
Ivan Danchev
Telerik team
answered on 27 Nov 2024, 03:26 PM

Hello Abdul,

We have an sample project that demonstrates this scenario in our repo with examples. 

Here's a link to the view where you can see the Grid's declaration including the Error event handler of the DataSource, which intercepts the error returned by the controller action: https://github.com/telerik/ui-for-aspnet-mvc-examples/blob/master/Telerik.Examples.Mvc/Telerik.Examples.Mvc/Areas/GridEditPopUpServerValidation/Views/Home/Index.cshtml

Here you can see the declaration of the action itself: https://github.com/telerik/ui-for-aspnet-mvc-examples/blob/master/Telerik.Examples.Mvc/Telerik.Examples.Mvc/Areas/GridEditPopUpServerValidation/Controllers/HomeController.cs

The name of the update action is Products_Update and it returns an error message saved in the ModelState:

 ModelState.AddModelError(/* Property name */ "Name", /* Validation message */ "My server error");

return Json(ModelState.ToDataSourceResult());


Regards,
Ivan Danchev
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Tags
Grid
Asked by
abdul
Top achievements
Rank 2
Iron
Answers by
Ivan Danchev
Telerik team
Share this question
or