We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Problem with OData in MVC Grid

Hello 

I'm trying to use MVC Grid with OData source. Source is in my solution and based on EF a ODataController. And Grid reads data but not put new, modyfied, deleted data into server :(
Grid uses only one action in contorller: public IQueryable<Driver> GetDrivers(), why grid doesn't call others actions ? 
Controler works with Postman, data was modyfiet after my own request. 
Plis show me what I'm doing wrong. 

My View

@model dynamic

@(Html.EJ().Grid<object>("DriversGrid")
    .Datasource("http://localhost:26168/odata/Drivers")
    .Columns(col =>
    {
        col.Field("Id").HeaderText("ID").IsIdentity(true).IsPrimaryKey(true).AllowFiltering(false).Add();
        col.Field("Name").HeaderText("Nazwa").Add();
        col.Field("DriverId").HeaderText("ID pastylki").Add();
    })
    .AllowFiltering()
    .FilterSettings(d => d.FilterType(FilterType.Menu))
    .AllowSorting()
    .ClientSideEvents(events =>
    {
        events.EndAdd("onEndAdd");
       // events.Load("onLoad");
    })
    .EditSettings(edit => { edit.AllowAdding().AllowDeleting().AllowEditing(); })
            .ToolbarSettings(toolbar =>
            {
                toolbar.ShowToolbar().ToolbarItems(items =>
                {
                    items.AddTool(ToolBarItems.Add);
                    items.AddTool(ToolBarItems.Edit);
                    items.AddTool(ToolBarItems.Delete);
                    items.AddTool(ToolBarItems.Update);
                    items.AddTool(ToolBarItems.Cancel);
                });
            })
)

<script>
    function onEndAdd(sender, args) {
        var gridObj = $("#DriversGrid").ejGrid("instance");
        gridObj.refreshContent();
    };
</script>

My odata contoller
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.ModelBinding;
using System.Web.Http.OData;
using System.Web.Http.OData.Routing;
using Reporter2.Models;

namespace Reporter2.Controllers
{
    /*
    The WebApiConfig class may require additional changes to add a route for this controller. Merge these statements into the Register method of the WebApiConfig class as applicable. Note that OData URLs are case sensitive.

    using System.Web.Http.OData.Builder;
    using System.Web.Http.OData.Extensions;
    using Reporter2.Models;
    ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
    builder.EntitySet<Driver>("Drivers");
    config.Routes.MapODataServiceRoute("odata", "odata", builder.GetEdmModel());
    */
    public class DriversController : ODataController
    {
        private ApplicationDbContext db = new ApplicationDbContext();

        // GET: odata/Drivers
        [EnableQuery]
        public IQueryable<Driver> GetDrivers()
        {
            return db.Drivers;
        }

        // GET: odata/Drivers(5)
        [EnableQuery]
        public SingleResult<Driver> GetDriver([FromODataUri] int key)
        {
            return SingleResult.Create(db.Drivers.Where(driver => driver.Id == key));
        }

        // PUT: odata/Drivers(5)
        public IHttpActionResult Put([FromODataUri] int key, Delta<Driver> patch)
        {
            Validate(patch.GetEntity());

            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            Driver driver = db.Drivers.Find(key);
            if (driver == null)
            {
                return NotFound();
            }

            patch.Put(driver);

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!DriverExists(key))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return Updated(driver);
        }

        // POST: odata/Drivers
        public IHttpActionResult Post(Driver driver)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            db.Drivers.Add(driver);
            db.SaveChanges();

            return Created(driver);
        }

        // PATCH: odata/Drivers(5)
        [AcceptVerbs("PATCH", "MERGE")]
        public IHttpActionResult Patch([FromODataUri] int key, Delta<Driver> patch)
        {
            Validate(patch.GetEntity());

            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            Driver driver = db.Drivers.Find(key);
            if (driver == null)
            {
                return NotFound();
            }

            patch.Patch(driver);

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!DriverExists(key))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return Updated(driver);
        }

        // DELETE: odata/Drivers(5)
        public IHttpActionResult Delete([FromODataUri] int key)
        {
            Driver driver = db.Drivers.Find(key);
            if (driver == null)
            {
                return NotFound();
            }

            db.Drivers.Remove(driver);
            db.SaveChanges();

            return StatusCode(HttpStatusCode.NoContent);
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }

        private bool DriverExists(int key)
        {
            return db.Drivers.Count(e => e.Id == key) > 0;
        }
    }
}


1 Reply

SS Seeni Sakthi Kumar Seeni Raj Syncfusion Team January 27, 2016 07:27 AM UTC

Hi Karol,

If we give the path directly or a string in dataSource property, it misbehaves for CRUD operations on Grid. We have created a new support incident under your account to track this issue. Please log on to our support website to check for further updates.

https://www.syncfusion.com/account/login?ReturnUrl=/support/directtrac/incidents

To work around this issue, assign the path in ej.DataManager’s Url property. Refer to the code example.

@(Html.EJ().Grid<object>("Grid")

    .Datasource(d => d.URL("http://localhost:54330/odata/Employees"))

        .  . . .

    .Columns(col => { col.Field("EmployeeID").IsPrimaryKey(true).Add();

            . . . . .

            })       
)


Regards,
Seeni Sakthi Kumar S.

Loader.
Live Chat Icon For mobile
Up arrow icon