Nullable values cannot be made null by OData Patch delta

I use Syncfusion.Blazor.Grid 19.1.0.56 for crud operations with OData V4 api.

I have some tables with foreign keys that are nullable, like :

    public partial class Company
    {
        [Key]
        public Guid Id { get; set; }
        public Guid? ClientId { get; set; }
        pulic string ClientName  { get; set; } //computed field
        [Required]
        [StringLength(50)]
        public string Name { get; set; }
        [StringLength(50)]
        
        [ForeignKey(nameof(ClientId))]
        [InverseProperty("Companies")]
        public virtual Client Client { get; set; }
    }

here some companies are owned by a Client other not.

I am having some minor problems with SfGrid and SfDropDownList,  in general i like them much.

Using SfGrid and SfDropDownList in GridColumn EditTemplate ,  i want to assign, change ore clear Client field of Companies.

I  use ODataV4Adaptor

          <SfDataManager Url="odata/Companies" Adaptor="Adaptors.ODataV4Adaptor" />


                <GridColumn Field=@nameof(Company.ClientName) [email protected] AutoFit="true">
                    <EditTemplate>
                    @{
                        var company = (context as Company);

                        <SfDropDownList ID="ClientId" TItem="Client" TValue="Guid?" @bind-Value=company.ClientId AllowFiltering="true" EnableVirtualization="true" FloatLabelType="FloatLabelType.Always" [email protected] ShowClearButton="true">
                            <SfDataManager Url="odata/Clients" Adaptor="Adaptors.ODataV4Adaptor" />
                            <DropDownListFieldSettings Text="CompanyName" Value="Id" />
                        </SfDropDownList>
                    }
                    </EditTemplate>
                </GridColumn>

There are 2 problems here:

1- By using Clear Button of SfDropDownList  i clear ClientId and save, but in OData Patch method i don't see field ClientId in changed properties. I checked in grid event OnActionBegin when saving changes that ClientId is changed to null as wanted, but the field is not sent to server api to be updated.

I had a similar problem with bool false values but setting [DefaultValue(null)] for non nullable fields solved that earlier problem. Here it does not work because the field is really nullable.

2- Even if i set ShowClearButton="true" on SfDropDownList  sometimes it does not show clear button, for example once popup (dropdown) is opened clear button disappears.

Problem 1# is more important, How can i make ODataV4Adaptor send all changes to server?

I hope you can solve this problem with adapter once and for all.

Thanks.

3 Replies 1 reply marked as answer

JP Jeevakanth Palaniappan Syncfusion Team April 22, 2021 03:57 PM UTC

Hi Ali, 
 
Greetings from Syncfusion support. 
 
Query 1: The field is not sent to server api to be updated. 
 
We have validated your query and we would like to inform you that the patch data has to be serialized to remove the properties which having the unchanged data and null data. So we suggest you to use the Put method to save the null values in the properties. We have prepared a sample to use the Put method. Please refer the below code snippet and the sample for your reference. 
 
 
FetchData.razor 
<SfGrid @ref="Grid1" TValue="WeatherForecast" AllowSorting="true" AllowFiltering="true" AllowPaging="true" Toolbar="@(new List<string>() { "Cancel", "Update" })"> 
              <SfDataManager @ref="dm" Url="/weatherforecast" Adaptor="Adaptors.ODataV4Adaptor"></SfDataManager> 
              <GridEditSettings AllowAdding="true" AllowDeleting="true" AllowEditing="true" Mode="EditMode.Normal"></GridEditSettings> 
              <GridColumns> 
    <GridColumn Field="@nameof(WeatherForecast.Id)" IsPrimaryKey="true"></GridColumn> 
    <GridColumn Field="@nameof(WeatherForecast.Summary)"></GridColumn> 
</GridColumns> 
</SfGrid> 
 
@code { 
    SfGrid<WeatherForecast> Grid1; 
    public SfDataManager dm { get; set; } 
    protected override void OnAfterRender(bool firstRender) 
    { 
        base.OnAfterRender(firstRender); 
        (dm.DataAdaptor as ODataAdaptor).Options.UpdateType = HttpMethod.Put; 
    } 
 
 
Controller 
        [HttpPut] 
        public async Task<IActionResult> Put([FromODataUri] int key, [FromBody] WeatherForecast update) 
        { 
            WeatherForecast entity = w.FirstOrDefault(x => x.Id == key); 
                                           if (entity != null) { 
                                                          entity.Id = update.Id; 
                                                          entity.Summary = update.Summary; 
                                                          entity.Num = update.Num; 
                                           }                                           
                                           return await Task.FromResult(Ok()); 
        } 
 
 
Query 2: if i set ShowClearButton="true" on SfDropDownList  sometimes it does not show clear button 
 
We have resolved the reported problem in the latest version. So kindly update the Syncfusion NuGet version to the latest version 19.1.57 to resolve this issue. 
 
Regards, 
Jeevakanth SP. 


Marked as answer

VB Vincent Bray November 13, 2023 05:42 PM UTC

Dear support,

It seems that the proposed solution is not working anymore (Grid v20..4.0.49)

Severity	Code	Description	Project	File	Line	Suppression State
Error (active) CS1612 Cannot modify the return value of 'ODataAdaptor.Options' because it is not a variable.

Do you have an alternative?

Regards

Vincent






MS Monisha Saravanan Syncfusion Team November 14, 2023 09:20 AM UTC


Hi Vincent,


Kindly use the below highlighted solution to change the Update Type as PUT in OdataV4 and revert us if you face any difficulties while implementing the below suggested solution.



 

<SfGrid @ref="Grid1" TValue="WeatherForecast" AllowSorting="true" AllowFiltering="true" AllowPaging="true" Toolbar="@(new List<string>() { "Cancel", "Update" })">

       <SfDataManager @ref="dm" Url="/weatherforecast" Adaptor="Adaptors.ODataV4Adaptor"></SfDataManager>

       <GridEditSettings AllowAdding="true" AllowDeleting="true" AllowEditing="true" Mode="EditMode.Normal"></GridEditSettings>

       <GridColumns>

...

   </GridColumns>

</SfGrid>

 

@code {

    SfGrid<WeatherForecast> Grid1;

    public SfDataManager dm { get; set; }

    protected override void OnAfterRender(bool firstRender)

    {

        base.OnAfterRender(firstRender);

        RemoteOptions Rm = (dm.DataAdaptor as ODataV4Adaptor).Options;

        Rm.UpdateType = HttpMethod.Put;

        (dm.DataAdaptor as ODataV4Adaptor).Options = Rm;

     

    }

}



Regards,

Monisha



Loader.
Up arrow icon