Should I create my search filters box like as a ViewComponent?

391 Views Asked by At

All examples that I found in web, use the ViewComponents for complex components that resolve itself, in other words, components that execute some logic and show some result without interact with the caller view.

But I have the search box bellow that will be used in some views and will should interact with the caller view

When the user to click in search button, the caller view will be load with the filtered data in a grid or table.

In asp.net webforms, I could create my own search filter box in a usercontrol, but in netcore, I don't know if I should use this new feature called ViewComponent for that.

what do you think?

Search Filtered Box

1

There are 1 best solutions below

1
On BEST ANSWER

There is not that true answer to this. My rule of thumb: If your views that use this search filter box share the same view model and it is not likely that the search box will be used later for very different views: start with a partial view. If the search box will be used on very different views with different view models: Implement a view component. View components are more independent from partial views since they have its own code in its corresponding class, so they can construct their own view models for example. Thus your search box will only be loosely coupled to your views and you won't have to alter so much outside your component if you want to adjust something later on. If you just want to save a piece of otherwise repeating cshtml code, a partial view will definitely suffice.