Odata V4 Delete Url in Blazor Grid

Hello,

I'm trying to setup Blazor (Client) Grid using OdataV4 API. My controller action look like this :

        [HttpDelete]
        public  IActionResult DeleteSessionFormation([FromODataUri] int key)
        {
            var sessionFormation = _context.SessionFormation.Where(s=>s.Id== key).FirstOrDefault();
            if (sessionFormation == null)
            {
                return NotFound();
            }

            _context.SessionFormation.Remove(sessionFormation);
             _context.SaveChanges();

            return Ok(sessionFormation);
        }

My model is : 

    public class SessionFormation
    {
        public int Id { get; set; }
        public DateTime StartDate { get; set; }
        public DateTime EndDate { get; set; }
        public int FiliereId { get; set; }
        public Filiere Filiere { get; set; }
        public List<StagiaireSessionFormation> StagiaireSessionFormation { get; set; }
    }


But the request url sent by the grid is something like this :

https://localhost:44380/odata/sessionformation('%7B%22Id%22:17,%22StartDate%22:%222020-06-09T02:00:00.000Z%22,%22EndDate%22:%222020-06-17T02:00:00.000Z%22,%22FiliereId%22:4,%22Filiere%22:null,%22StagiaireSessionFormation%22:null,%22BlazId%22:%22BlazTempId_9964dbc5-9b88-4d1d-b4e7-e2890f1a3b54%22%7D')

Seems like it is trying to pass the whole object instead of just the Id...

Can you help me with this one ?

Thanks

3 Replies 1 reply marked as answer

VN Vignesh Natarajan Syncfusion Team June 16, 2020 11:31 AM UTC

Hi Cedeg,  
 
Greetings from Syncfusion support.  
 
Query: “Seems like it is trying to pass the whole object instead of just the Id... 
 
From your query we understand that you are facing issue while deleting a record from ODataV4 service using Grid control. As we need some more details about your issue. Kindly share the following details.  
 
  1. Share your Grid rendering code example.  
  2. Kindly ensure that you have defined IsPrimaryKey property to column with unique value.
  3. Share your Syncfusion Nuget package version details.
  4. Are using facing above issue while using built in delete toolbar or DeleteRecord method?
  5. If possible share the issue reproducible sample.
 
Regards, 
Vignesh Natarajan.  



CE Cedeg June 16, 2020 12:43 PM UTC

Hello,

Thanks for your quick answer.

Here is the grid rendering code :

<SfGrid Width="95%" TValue="SessionFormation" AllowGrouping="true" AllowPaging="true" Toolbar="@(new List<string>() {"Add", "Edit", "Delete","Cancel", "Update" })" Height="500px" @ref="SessionFormationGrid">
    <GridEditSettings AllowEditing="true" AllowAdding="true" AllowDeleting="true"></GridEditSettings>
    <GridGroupSettings Columns="@(new string[]{"FiliereId"})">
    </GridGroupSettings>
    <SfDataManager Url="@(Configuration["ApiHost"]+"/odata/sessionformation?$expand=Filiere,StagiaireSessionFormation($expand=Stagiaire)" )" Adaptor="Adaptors.ODataV4Adaptor"></SfDataManager>
    <GridColumns>
        <GridColumn Field="StartDate" HeaderText="Début de la formation" Width="110" EditType="EditType.DateTimePickerEdit"></GridColumn>
        <GridColumn Field="EndDate" HeaderText="Fin de la formation" Width="110" EditType="EditType.DateTimePickerEdit"></GridColumn>
        <GridForeignColumn TValue="Filiere" Field="FiliereId" HeaderText="Formation" ForeignKeyField="Id" ForeignKeyValue="Name" Width="150">
            <SfDataManager Url="@(Configuration["ApiHost"] + "/odata/filiere")" Adaptor="Syncfusion.Blazor.Adaptors.ODataV4Adaptor" CrossDomain="true"></SfDataManager>
        </GridForeignColumn>
    </GridColumns>
</SfGrid>

My primary key is Id and yes it's unique (generated by entity framework)

I'm using the last version (18.1.0.57)

The issue occurs only when using the toolbar, i ended up adding a column with a delete button and calling the API directly, but i would greatly prefer using the buitl-in features of the grid.


VN Vignesh Natarajan Syncfusion Team June 17, 2020 01:32 PM UTC

Hi Cedeg, 
 
Thanks for sharing the requested details. 
 
Query: “eems like it is trying to pass the whole object instead of just the Id... 
 
Yes. We are able to reproduce the reported issue at our end while preparing a sample using your code example. In your code example you have not defined the IsPrimaryKey property to any of the available column in SfGrid definition. To perform CRUD operation in Grid, primaryKey column must defined to available column (GridColumn definition in SfGrid) whose value is unique. Based on the primaryKey value only CRUD operation will take place in Grid.   
 
Refer the below code example. (since Id column is primaryKey in your ODataV4 service) 
 
<SfGrid Width="95%" TValue="SessionFormation" AllowGrouping="true" AllowPaging="true" Toolbar="@(new List<string>() {"Add""Edit""Delete","Cancel""Update" })" Height="500px" @ref="SessionFormationGrid"> 
    <GridEditSettings AllowEditing="true" AllowAdding="true" AllowDeleting="true"></GridEditSettings> 
    <GridGroupSettings Columns="@(new string[]{"FiliereId"})"> 
    </GridGroupSettings> 
    <SfDataManager Url="@(Configuration["ApiHost"]+"/odata/sessionformation?$expand=Filiere,StagiaireSessionFormation($expand=Stagiaire)" )" Adaptor="Adaptors.ODataV4Adaptor"></SfDataManager> 
    <GridColumns> 
        <GridColumn Field="Id" IsPrimaryKey="true" IsIdentity="true" Visible="false"></GridColumn> 
        <GridColumn Field="StartDate" HeaderText="Début de la formation" Width="110" EditType="EditType.DateTimePickerEdit"></GridColumn> 
        <GridColumn Field="EndDate" HeaderText="Fin de la formation" Width="110" EditType="EditType.DateTimePickerEdit"></GridColumn> 
        <GridForeignColumn TValue="Filiere" Field="FiliereId" HeaderText="Formation" ForeignKeyField="Id" ForeignKeyValue="Name" Width="150"> 
            <SfDataManager Url="@(Configuration["ApiHost"] + "/odata/filiere")" Adaptor="Syncfusion.Blazor.Adaptors.ODataV4Adaptor" CrossDomain="true"></SfDataManager> 
        </GridForeignColumn> 
    </GridColumns> 
</SfGrid> 
 
 
Note: If you have autoincrement primaryKey column in your service, then kindly define the IsIdentity property of GridColumn as true to disable the primaryKey column while inserting a record.    
 
Refer our UG documentation for your reference 
 
 
Kindly get back to us if you have further queries.  
 
Regards, 
Vignesh Natarajan  
 
   
 
 


Marked as answer
Loader.
Up arrow icon