my page is razor,access controller error 415

1 Answer 44 Views
Grid
ss
Top achievements
Rank 1
ss asked on 09 Nov 2023, 01:14 AM

page:

var dataSource = new kendo.data.DataSource({
                type: "aspnetmvc-ajax",
                pageSize: 25000,
                serverPaging: false,
                serverGrouping: false,
                transport: {
                    read: {
                        url: urlRead,
                        data: kendo.antiForgeryTokens(),
                        type:"GET"
                    },

                    create: {
                        url: urlCreate,
                        data: kendo.antiForgeryTokens(),
                        cache: true,

                    },
                    update: {
                        url: urlUpdate,
                        data: kendo.antiForgeryTokens(),
                        cache: true,
                    },
                    destroy: {
                        url: urlDestroy,
                        data: kendo.antiForgeryTokens(),
                        cache: true
                    },

                    parameterMap: function (data, type) {
                        //if (type == "create") {
                        //    // send the created data items as the "models" service parameter encoded in JSON
                        //    return { models: kendo.stringify(data.models) };
                        //}
                    }

                },
                //requestStart: function() {
                //    kendo.ui.progress($("#"+gridName), true);
                //},
                //requestEnd: function() {
                //    kendo.ui.progress($("#"+gridName), false);
                //},
                sync: function (e) {


                },
                schema: {
                    data: "Data",
                    total: "Total",
                    errors: "Errors",
                    model: model,
                    errors: function (response) {
                        //ShowNotification(response);
                        return response.error;
                    }
                },
                error: function (e) {
                    //ShowNotification("Error");
                    dataSource.cancelChanges();
                }
            });
            //dataSource.bind("error", function () { ShowNotification("dsError") });

            grid.setOptions({
                dataSource: dataSource,
                persistSelection: true,
                navigatable: true,
                resizable:true,
                reorderable:true,
                dataBound: function (e) {
                    if (e.sender.dataSource.view().length > 0) {
                        e.sender.select("tr:eq(0)");
                        grid.trigger("change");
                        grid.unbind("dataBound");
                    }

                }
            });

backend: Controller

[HttpPost("AddCoatingType")]
        public ActionResult AddCoatingType(CoatingAgentType item,[DataSourceRequest] DataSourceRequest request )
        {
            try
            {
                //var experimentItemsView = ApplicationHelper.GetExperimentitems(AppConstant.ISTA, _ipConfig);
                //_coatingAgentTypeRepository.Insert(coatingAgentType);
                //await _coatingAgentTypeRepository.SaveAsync();
                //return new JsonResult(new[] { coatingAgentType }.ToDataSourceResult(request, ModelState));
                return new JsonResult("OK");

            }
            catch (Exception ex)
            {
                return this.StatusCode(StatusCodes.Status500InternalServerError, $"Error Info: {ex.Message}");
            }
        }                        

1 Answer, 1 is accepted

Sort by
0
Mihaela
Telerik team
answered on 13 Nov 2023, 08:20 AM

Hello,

I would recommend updating the type of the Read request to "POST" and updating the handler method name with the OnPost prefix:

    var dataSource = new kendo.data.DataSource({
        type: "aspnetmvc-ajax",
        pageSize: 25000,
        serverPaging: false,
        serverGrouping: false, 
        transport: {
          read: {
            url: '@Url.Page("Page1","AddCoatingType")',
            data: kendo.antiForgeryTokens(),
            type: "POST"
          },
          ...
});

//PageModel (i.e., "Page1.cshtml.cs")
public JsonResult OnPostAddCoatingType([DataSourceRequest] DataSourceRequest request)
{
   ...
   var dataCollection = new List<GridViewModel>() {...};
   return new JsonResult(dataCollection.ToDataSourceResult(request));
}

For more information, refer to the DataSource in Razor Pages scenario documentation.

 

Regards,
Mihaela
Progress Telerik

Stay tuned by visiting our public roadmap and feedback portal pages. If you're new to the Telerik family, be sure to check out our getting started resources, as well as the only REPL playground for creating, saving, running, and sharing server-side code.
Tags
Grid
Asked by
ss
Top achievements
Rank 1
Answers by
Mihaela
Telerik team
Share this question
or