Grid ADD/EDIT/DELETE?

I am using a grid with odatav4 adapter. Grid loads data ok, but I don't know how to write the methods in the controller so that they will be called by the grid (to add/edit/delete)

here's Grid Code:
@(Html.EJ().Grid<Resource>("ResourcesGrid")
          .Datasource(ds => ds.URL(AppSettings.ApiUrl + "Resources").Adaptor(AdaptorType.ODataV4Adaptor).CrossDomain(true))
          .AllowSorting()
          .AllowPaging()
          .AllowFiltering()
          .AllowGrouping()
          .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);
              });
          })
          .FilterSettings(f => f.FilterType(FilterType.Menu))
          .PageSettings(p => p.PageSize(15))
          .IsResponsive()
          .Columns(col =>
          {
              col.Field("Id").Width(300).IsPrimaryKey(true).Visible(false).Add();
              col.Field("Type.Name").HeaderText("Tip").TextAlign(TextAlign.Right).Width(100).Add();
              col.Field("Type.Id").Visible(false).Add();
              col.Field("Value").HeaderText("Valoare").Priority(1).Add();
              col.Field("ExternalId").HeaderText("Id Extern").Width(150).Priority(4).Add();
          }))


here's controller code:
    public class ResourcesController : ODataController
    {
        

     ?????????????????????????????????????????????????

        [EnableQuery]
        public PageResult<Resource> Get(ODataQueryOptions<Resource> options)
        {
            var resourceReader = new ResourceReader();
            IQueryable<Resource> resources = resourceReader.GetResources();
            var count = resources.Count();
            options.ApplyTo(resources);

            return new PageResult<Resource>(resources, null, count);
        }

      
    }

4 Replies

VA Venkatesh Ayothi Raman Syncfusion Team January 11, 2018 10:58 AM UTC

Hi Catalin, 

Thanks for using Syncfusion products. 

We have created a sample based on your requirement which can be download from following link, 


In this sample, we have rendered the Grid with ODataV4Adaptor and write the methods for add/edit/delete in OData controller. Please refer to the following code example, 
@(Html.EJ().Grid<object>("Editing") 
                .Datasource(ds => { ds.URL("/odata/Orders").Adaptor(AdaptorType.ODataV4Adaptor); }) 
 
 
         .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); 
                }); 
            }) 
             
                 
                .AllowPaging() 
        .Columns(col => 
        { 
            col.Field("OrderID").HeaderText("Order ID").IsPrimaryKey(true).IsIdentity(true).Width(50).Add(); 
            col.Field("EmployeeID").HeaderText("Employee ID").Width(50).Add(); 
            col.Field("ShipCity").HeaderText("ShipCity").Width(50).Add(); 
 
        }) 
) 
 
[OData controller] 
 
// PUT odata/Orders(5) 
        public async Task<Order> Put(int key, Order order) //Edit operation in database 
        { 
             
           var entity =  await db.Orders.FindAsync(order.OrderID); 
           db.Entry(entity).CurrentValues.SetValues(order); 
            await db.SaveChangesAsync(); 
            return order; 
        } 
        //// POST odata/Orders 
        public async Task<Order> Post(Order order) //Add Operation in database 
        { 
             
            db.Orders.Add(order); 
            await db.SaveChangesAsync(); 
            return order; 
        } 
 
        
        //// DELETE odata/Orders(5) 
        public async Task<IHttpActionResult> Delete([FromODataUri] int key) 
        { 
 
            var od = await db.Orders.FindAsync(key); 
            if (od == null) 
            { 
                return NotFound(); 
            } 
 
            db.Orders.Remove(od); 
            await db.SaveChangesAsync(); 
            return StatusCode(HttpStatusCode.NoContent); 
        }  
 

Please let us know if you have any further assistance on this. 

Regards, 
Venkatesh Ayothiraman. 



CR Catalin Radoi January 11, 2018 02:21 PM UTC

Thanks, Venkatesh, have a nice day!



CR Catalin Radoi January 11, 2018 03:32 PM UTC

Unfortunately, the adding doesn't work :(






Also, the record I am trying to add, has a foreign key to another record TypeId (hidden in the grid - only the Type Name is shown). How can I also add the type? A dropdown or something in the grid, while adding? I'm completely lost :(


MP Manivannan Padmanaban Syncfusion Team January 12, 2018 12:59 PM UTC

Hi Catalin, 
 
Sorry for the inconvenience caused. 
 
We are unable to reproduce the reported issue at our end please provide the following details, 
 
1.Do you need to add the foreign record? 
2.Please share the exact scenario you are facing. 
3.Provide the complete code snippet of model and view controller. 
4.Provide the issue reproducing sample 
 
The provided details help as to resolve the issue as soon as possible. 
 
Regards, 
Manivannan Padmanaban.  


Loader.
Up arrow icon