0

I am trying to implement the Syncfusion Blazor QueryBuilder component to build dynamic search filters.
I can successfully store query builder rules to my DB after mapping to my C# class model. But when I try to re-map these rules back to the Syncfusion "RuleModel" class I get error below in the browser. It appears to be caused by the dynamic property types on the "Operate" and "Value" fields.
When I get the error, these properties have the "ValueKind" element. When this is not present, it works fine (eg. If I manually create a new RuleModel())

Error in Browser, when QueryBuilderObj.SetRules() method is called..

Error: Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: Cannot implicitly convert type 'System.Text.Json.JsonElement' to 'string'
   at CallSite.Target(Closure , CallSite , Object )
   at System.Dynamic.UpdateDelegates.UpdateAndExecute1[T0,TRet](CallSite site, T0 arg0)
   at Syncfusion.Blazor.QueryBuilder.Internal.QueryBuilderRules`1.SetField()
   at Syncfusion.Blazor.QueryBuilder.Internal.QueryBuilderRules`1.OnParametersSetAsync()
   at Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion(Task task)   

Client Code


<SfQueryBuilder TValue="@FilterColumns" @ref="QueryBuilderObj" MaxGroupCount=3>
    <QueryBuilderColumns>
       <QueryBuilderColumn Field="Status" Label="Status" Type="ColumnType.String"></QueryBuilderColumn>
       <QueryBuilderColumn Field="DepartmentCode" Label="DepartmentCode" Type="ColumnType.String"></QueryBuilderColumn>
    </QueryBuilderColumns>
</SfQueryBuilder>
<button type="button" @onclick="getRules">Get Rules</button>

@code {
    SfQueryBuilder<FilterColumns> QueryBuilderObj;

    public class FilterColumns
    {
        public string Status { get; set; }
        public string DepartmentCode { get; set; }
    }

    [Parameter]
    public RuleModel rules { get; set; }

    private void getRules()
    {
        QueryBuilderObj.SetRules(rules.Rules, rules.Condition);
    }

}

Syncfusion RuleModel Class

public class RuleModel
{
        public RuleModel();

        public string Condition { get; set; }
        public string Field { get; set; }
        public string Label { get; set; }
        public bool? Not { get; set; }
        public dynamic Operator { get; set; }
        public string Type { get; set; }
        public dynamic Value { get; set; }
        public List<RuleModel> Rules { get; set; }
}

I have replicated the syncfusion RuleModel class exactly as above in my domain model.

Has anyone successfully stored and retrieved QueryBuilder rules from Blazor UI into a C# model/class?

Thanks.

PDC
  • 58
  • 6

1 Answers1

0

I figured out this problem is due to System.Text.Json behaviour causing the newly created property to have a ValueKind object on each property.

Basically this problem... How do I get System.Text.Json to deserialize objects into their original type?

When I switch to using NewtonSoft JSON.NET to de-serialize the RuleModel, it solved the issue.
Not sure how to make it work with System.Text.Json - but will use Json.Net for now.

PDC
  • 58
  • 6