Base solution for your next web application
Open Closed

Ajax response changed in aspnetzero(MVC) #2532


User avatar
0
jatingadhiya created

Hello,

I am using kendo UI controls in aspnetzero(MVC). And kendo controls accept json response from controller.

When i am integrating kendo control with simple mvc project then json reponse is: {"data":[{"id":2,"firstname":"Jatin","lastname":"Gadhiya"}],"total":1,"aggregateResults":null,"errors":null}

But i am integrating with aspnetzero then json response is: {"result":{"data":[{"id":2,"firstname":"jatin","lastname":"gadhiya","isdeleted":false,"deleteruserid":null,"deletiontime" :null,"lastmodificationtime":null,"lastmodifieruserid":null,"creationtime":"2001-01-01t00:00:00z","creatoruserid" :null}],"total":1,"aggregateresults":null,"errors":null},"targeturl":null,"success":true,"error":null ,"unauthorizedrequest":false,"__abp":true}

So its binding my result(data) inside "result". then my control not showing any result. Because not getting proper response from controller side.

One more thing its making capitalize to small also.

Please help me what i have to do for change my response. or any other solution?

Awaiting for your response.

Thanks, Jatin


5 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    You can use DontWrapResult attribute if you don't want ABP to change your result. You can also check your KendoUI sample here <a class="postlink" href="https://github.com/aspnetboilerplate/aspnetboilerplate-samples/tree/master/KendoUiDemo">https://github.com/aspnetboilerplate/as ... endoUiDemo</a>

  • User Avatar
    0
    jatingadhiya created

    Yes its work.

    Now i am getting this error: NetworkError: 400 Empty or invalid anti forgery header token. - <a class="postlink" href="http://localhost:6240/Mpa/Contacts/ContactList">http://localhost:6240/Mpa/Contacts/ContactList</a>"

    here is my code ContactsController:

    [DontWrapResult]
        public class ContactsController : AbpZeroTemplateControllerBase
        {
       public ActionResult Index()
            {
                //var model = _contactsService.Read();
    
                return View();
            }
    
        [HttpPost]
            public ActionResult ContactList(DataSourceRequest command)
            {
                var contacts = _contactsService.GetAllContacts(command.Page - 1,
                  pageSize: command.PageSize);
    
    
                var contactsGrid = contacts
                    .Select(x =>
                    {
                        var gModel = new ContactsModel
                        {
                            Id = x.Id,
                            Firstname = x.Firstname
                        };
    
                        return gModel;
                    })
                    .ToList();
                var gridModel = new DataSourceResult
                {
                    Data = contactsGrid,
                    Total = contactsGrid.Count
                };
                return Json(gridModel);
            }
    
    }
    

    here is the view page index.cshtml

    @{
        ViewBag.Title = "Index";
        Layout = "~/Areas/Mpa/Views/Layout/_Layout.cshtml";
    }
    
    <div class="row margin-bottom-5">
        <div class="col-xs-6">
            <div class="page-head">
                <div class="page-title">
                    <h1>
                        <span>@L("Contacts")</span>
                    </h1>
                </div>
            </div>
        </div>
    
    </div>
    <div class="portlet light margin-bottom-0">
        <div class="portlet-body">
            <div id="grid"></div>
            </div>
        </div>
           
    
            <script>
                $("#grid").kendoGrid({
                    dataSource: {
                        //type: "json",
                        transport: {
                            read: {
                                url: "@Html.Raw(Url.Action("ContactList", "Contacts"))",
                            type: "POST",
                            dataType: "json"
                           
                        }
                },
                schema: {
                        data: "Data",
                        total: "Total",
                        errors: "Errors",
                        model: {
                        id: "Id",
                        fields: {
                                Id: { editable: false, type: "number" },
                                Firstname: { editable: true, type: "string" }
                             
                            }
                        }
                },
                requestEnd: function (e) {
                    if (e.type == "create" || e.type == "update") {
                        this.read();
                    }
                },
                error: function (e) {
                  //  display_kendoui_grid_error(e);
                    // Cancel the changes
                    this.cancelChanges();
                },
                serverPaging: true,
                    serverFiltering: true,
                serverSorting: true
                },
                pageable: {
                        pageSize: 6,
                        refresh: true,
                        numeric: true,
                        previousNext: true,
                        info: true
                },
                toolbar: ["create"],
                    editable: {
                    confirmation: false,
                    mode: "inline"
                    },
                scrollable: true,
                    columns: [{
                        field: "Id",
                        title: "Id",
                        width: 120,
                        hidden: true
                    },
    
                    { field: "Firstname", title: "First Name", width: 100 },
                    {
                        command: [{
                            name: "edit",
                            text: "Edit"
                        }, {
                            name: "destroy",
                            text: "Delete"
                        }],
                        width: 160
                    }]
                });
            </script>
    
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    Did you try to put DontWrapResult attribute to ContactList method and remove it from the Controller ?

  • User Avatar
    0
    jatingadhiya created

    Yes I try still same error

  • User Avatar
    0
    ismcagdas created
    Support Team

    [DontWrapResult] attribute should not cause this problem. So, when you remove [DontWrapResult] attribute from your Controller and action, does this error go away ?