Declined
Last Updated: 04 Apr 2025 11:51 by DRASKO
DRASKO
Created on: 28 Mar 2025 12:16
Category: UI for Blazor
Type: Bug Report
1
Setting Form model in OnParametersSet causes an infinite loop

Hello,

related to my previous bug report.

When Add / Edit is clicked in the Grid, it causes a Dialog to open. 

The dialog has a Form, and if the model for that form is set through OnParametersSet, the method is called in an infinite loop, and the Dialog never gets shown.

Attached Files:
2 comments
DRASKO
Posted on: 04 Apr 2025 11:51

Hello Nadezhda,

Thank you for the clarification!

 

ADMIN
Nadezhda Tacheva
Posted on: 04 Apr 2025 09:48

Hello DRASKO,

I revised the attached application in detail and it looks to me that the behavior is a matter of configuration specifics rather than a bug. Let me provide more details and a suggestion on how to solve it.

The OnParametersSet method in Blazor is called whenever a component's parameters are set or updated. In your case, the infinite loop is likely caused by the following line in the OnParametersSet method:

Model = mapper.Map<AddCameraModel>(EditedCamera);

This line updates the Model property every time OnParametersSet is called. If Model is bound to the UI and any change to Model triggers a re-render, this will cause OnParametersSet to be called again, creating an infinite loop.

To break this loop, you should only update Model if EditedCamera has actually changed. You can achieve this by comparing the current EditedCamera with the previous one and only updating Model if they are different.

Here is an updated version of the OnParametersSet method:

private CameraDTO? _previousEditedCamera;

protected override void OnParametersSet()
{
    _onParametersSetCalled++;

    Console.WriteLine($"OnParametersSet called {_onParametersSetCalled} times");

    if (EditedCamera != null && !EditedCamera.Equals(_previousEditedCamera))
    {
        Model = mapper.Map<AddCameraModel>(EditedCamera);
        _previousEditedCamera = EditedCamera;
    }
}

This change ensures that Model is only updated when EditedCamera actually changes, preventing the infinite loop.

Having the above in mind, I am marking the current item as "Declined" since it is not a valid bug report.

===

Additional tip:

You have another ticket with the same configuration where you are setting the model in the OnAfterRender. Currently, it is not clear to me why you are setting the model in a different lifecycle events than in the OnInitialized, so I'd like to share some good practices:

  • Using the OnParametersSet can be useful if the model depends on changing parameters.
  • Using OnInitialized is generally recommended if the model should only be set once. In your case, you are opening an instance of the AddCameraDialog component once per each record, so I believe you do not need to change the model once you open the dialog. In this case, I recommend using the OnInitialized method to set the model.
  • Using OnAfterRender is not usually a good practice unless you have a specific reason for doing so. OnAfterRender is triggered after the component has rendered, which can cause unnecessary re-renders if the model is updated. If you set the model in OnAfterRender, you may trigger a state change that forces Blazor to render the component again, leading to potential performance issues.

Regards,
Nadezhda Tacheva
Progress Telerik

Enjoyed our products? Share your experience on G2 and receive a $25 Amazon gift card for a limited time!