Request URL is not in Odata format

2 posts, 0 answers
  1. Markus
    Markus avatar
    4 posts
    Member since:
    Apr 2013

    Posted 07 Apr 2013 Link to this post

    Hi,

    We have been struggling to implement a kendo-ui grid for several days consuming data from a WCF service proxy.  All of our data tests in Fiddler work correctly and if we hard code a request in our Get action in the controller we can see the service is working and our dataset it retrieved.  We think the issue is that the URL is request is not an Odata request. I have looked through to see if there is an Html helper to specify Odata in the index.chtml grid builder but have not found anything.  We are pretty close to giving up on Kendo-ui completely  I feel like this should be much easier to implement.

    index.chtml
    @model IEnumerable<Application.ServiceProxy.CustomerDTO>
     
    @(Html.Kendo().Grid(Model)   
        .Name("grid")
        .Columns(columns => {
            columns.Bound(c => c.customerID).Width(100);
            columns.Bound(c => c.name1).Width(100);
            columns.Bound(c => c.matchcode).Width(100);
            columns.Bound(c => c.city).Width(100);
            columns.Bound(c => c.postalCode).Width(100);
        })
         
        .Pageable()
        .Sortable()
        .Scrollable()
        .Filterable()
        //.HtmlAttributes(new { style = "height:430px;" })
        .DataSource(dataSource => dataSource
            .Ajax()
            .PageSize(20)
            .Read(read => read.Action("Get", "Customer")   
    )




    Customer Controller


    using System;
    using System.Collections.Generic;
    using System.Net.Http;
    using System.Web.Mvc;
    using Kendo.Mvc.UI;
    using Kendo.Mvc.Extensions;
    using BusinessLogic.Models;
    using System.Web;
    using System.Linq;
    using Newtonsoft.Json;
    using Application.ServiceProxy;
    using System.Web.Http;
     
    using System.Web.Http.OData.Query;
    using System.Web.Http.OData;
     
    namespace Application.Controllers
    {
        public class CustomerController : Controller
        {
     
            [Queryable(AllowedQueryOptions = AllowedQueryOptions.All)]
            public IEnumerable<ServiceProxy.CustomerDTO> Get()
            {
                    var odataService = new Container(new Uri("http://localhost:8089/odata/"));
     
                var customer = odataService.Customer.Take(10);
                return customer.AsEnumerable();
            }
     
            public ActionResult Index() {
                return View() ;
            }
     
        }
     
    }









    BusinessLogic  customer controller (providing OData)

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Web;
    using System.Net.Http;
    using System.Web.Http;
    using BusinessLogic.Models;
    using System.Web.Http.OData.Query;
    using System.Web.Http.OData;
     
     
     
    namespace BusinessLogic.Controllers
    {
        //public class CustomerController : CPSGlobalAPIController
        public class CustomerController : EntitySetController<CustomerDTO, int>
        {
            protected cpsGlobalEntities entity = new cpsGlobalEntities();
            #region mapper
     
            private IQueryable<CustomerDTO> mapCustomer()
            {
                return
                    from c in entity.customer
                    select new CustomerDTO()
                    {
                        customerID = c.customerID,
                        matchcode = c.matchcode,
                        salesOrganizationCompanyCode = c.salesOrganization.companyCode,
                        salesOrganizationID = c.salesOrganizationID,
                        salesOrganizationName = c.salesOrganization.name,
                        statusID = c.statusID,
                        statusPrefix = c.status.prefix,
                        statusTranslationKey = c.status.translationKey,
                        serviceLevelStatusID = c.serviceLevelStatusID,
                        serviceLevelPrefix = c.status1.prefix,
                        serviceLevelTranslationKey = c.status1.translationKey,
                        systemID = c.systemID,
                        dunsNumber = c.dunsNumber,
                        mediaIDLogo = c.mediaIDLogo,
                        salesOrganization = c.salesOrganization.companyCode,
                        inventoryManagerID = c.inventoryManager.inventoryManagerID,
                        inventoryManagerAddressID = c.inventoryManager.addressID,
                        customerExternalSystemID = c.customerExternalSystemID,
                        internationalGroup = c.internationalGroup,
                        division = c.division,
                        corporateID = c.corporateID,
     
     
     
                        #region duplicated items for improved list view
                        name1 = c.customerExternalSystem.address.name1,
                        postalCode = c.customerExternalSystem.address.postalCode,
                        city = c.customerExternalSystem.address.city,
                        stateCode = c.customerExternalSystem.address.state.stateCode,
                        countryCode = c.customerExternalSystem.address.country.countryCode
                        #endregion
     
                    };
            }
     
            #endregion
     
            #region GET
            #region GET all
             [Queryable]
            public override IQueryable<CustomerDTO> Get()
            {
                return mapCustomer().AsQueryable();
            }
            #endregion
     
            #region GET by ID
            protected override CustomerDTO GetEntityByKey(int key)
            {
                //return mapCustomer().FirstOrDefault(p => p.customerID == key);
     
                var cust = (from c in mapCustomer() where c.customerID == key select c).FirstOrDefault();
                if (cust.inventoryManagerAddressID != null)
                {
                    AddressController adC = new AddressController();
                    cust.inventoryManagerAddressDTO = adC.Get((int)cust.inventoryManagerAddressID);
                }
                if (cust.customerExternalSystemID != null)
                {
                    CustomerExternalSystemController cesC = new CustomerExternalSystemController();
                    cust.customerExternalSystemDTO = cesC.Get((int)cust.customerExternalSystemID);
                }
                if (cust == null)
                    throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
                return cust;
            }
     
            
            #endregion
     
            #region POST
     
            protected override CustomerDTO CreateEntity(CustomerDTO customerDTO)
            {
                var customer = new customer()
                {
                    matchcode = customerDTO.matchcode,
                    salesOrganizationID = customerDTO.salesOrganizationID,
                    statusID = customerDTO.statusID,
                    serviceLevelStatusID = customerDTO.serviceLevelStatusID,
                    mediaIDLogo = customerDTO.mediaIDLogo,
                    systemID = customerDTO.systemID,
                    customerExternalSystemID = customerDTO.customerExternalSystemID,
                    internationalGroup = customerDTO.internationalGroup,
                    division = customerDTO.division,
                    corporateID = customerDTO.corporateID,
                    inventoryManagerID = customerDTO.inventoryManagerID,
                    dunsNumber = customerDTO.dunsNumber
                };
     
                entity.customer.Add(customer);
                entity.SaveChanges();
     
                return GetEntityByKey(customer.customerID);
            }
     
            protected override int GetKey(CustomerDTO customer)
            {
                return customer.customerID;
     
            }
     
            
            #endregion
     
            #region Put
            protected override CustomerDTO UpdateEntity(int key, CustomerDTO customerDTO)
            {
     
                var originalCustomer = entity.customer.Find(key);
                originalCustomer.matchcode = customerDTO.matchcode;
                originalCustomer.salesOrganizationID = customerDTO.salesOrganizationID;
                originalCustomer.statusID = customerDTO.statusID;
                originalCustomer.serviceLevelStatusID = customerDTO.serviceLevelStatusID;
                originalCustomer.mediaIDLogo = customerDTO.mediaIDLogo;
                originalCustomer.systemID = customerDTO.systemID;
                originalCustomer.customerExternalSystemID = customerDTO.customerExternalSystemID;
                originalCustomer.inventoryManagerID = customerDTO.inventoryManagerID;
                originalCustomer.dunsNumber = customerDTO.dunsNumber;
                originalCustomer.internationalGroup = customerDTO.internationalGroup;
                originalCustomer.division = customerDTO.division;
                originalCustomer.corporateID = customerDTO.corporateID;
     
                entity.SaveChanges();
                return GetEntityByKey(key);
            }
     
     
            #endregion
     
            #region PATCH
     
            #endregion
     
            #region DELETE
     
            public override void Delete([FromODataUri] int key)
            {
                var customer = entity.customer.Find(key);
                if (customer == null)
                {
                    throw new HttpResponseException(HttpStatusCode.NotFound);
                }
                entity.customer.Remove(customer);
                entity.SaveChanges();
            }
     
                   #endregion
     
     
        }
    }


  2. Atanas Korchev
    Admin
    Atanas Korchev avatar
    8462 posts

    Posted 08 Apr 2013 Link to this post

    Hello Markus,

    Your action method needs to return data in the format expected by the grid. You can find more info here: http://docs.kendoui.com/getting-started/using-kendo-with/aspnet-mvc/helpers/grid/ajax-binding

    Basically you need to call the ToDataSourceResult extension method:

    public IEnumerable<ServiceProxy.CustomerDTO> Get([DataSourceRequest] DataSourceRequest request)
            {
                    var odataService = new Container(newUri("http://localhost:8089/odata/"));
     
                var customer = odataService.Customer.Take(10);
                return customer.ToDataSourceResult(request);
            }

    All the best,
    Atanas Korchev
    the Telerik team
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
Back to Top