-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Should be able to return ViewComponents from controller actions (aka ViewComponent ActionResult helper) #2338
Comments
Why not use a partial? |
Treating ViewComponents as the replacement for Child Actions; in other words, leverage controller logic via the full MVC rather than just the View (as Partial would be). The best use case I'm thinking of is partial rendering: render the ViewComponent as part of the full page for the first request, then call another method to render just that ViewComponent as a partial. Key point here is that it's not just rendering a partial view, but the ViewComponent's controller is responsible for building the component's model (perhaps via DB calls or something). That means there is one single place in the code that this thing is built (data access and all), and I'm not duplicating logic across different controller actions (one for the main load controller action which may be doing much more than just what's needed for the partial, and again in the partial rendering controller action). Maybe I'm just abusing or misunderstanding the point of the ViewComponent. :) |
This is exactly what view component does, except there is no controller involved. You do all the async work in the C# part of the view component and the view work in the cshtml part. And there is no code duplication. |
@yishaigalatzer I'm not following. Can you point me to a code sample of how to render a ViewComponent in response to an AJAX/partial rendering request without creating a view to render it in? |
You can't. That piece was never meant to work. But I see the appeal of your idea now. Basically one less hop, by inferring the return type. It's a bit tricky with viewcomponents that don't return html but not too bad of a concept. Probably need an overload that takes content type, and need a way to take parameters, so perhaps a lambda style API might work. |
Is it possible to re-invoke a view component? I'm building a master->detail view with master being search results of registrants, and detail being a selected registrant's details. I am conceiving the details view as a view component (as I would a user control in webforms). My question is how to refresh/update the details view upon the selection of a different registrant in the search results (initial call from @await Component.InvokeAsync("SearchRegistrantDetails", "RegistrationID") |
You'll need some client-side code to make this work if selection is happening in the user's browser. Once this issue is fixed:
View components aren't a resurrection of webforms. You might want to think too about just moving this to an api with data binding happening on the client. |
Makes it easier to render a view component from inside a controller. This makes it possible to implement behavior where an ajax request refreshes part of a page.
Makes it easier to render a view component from inside a controller. This makes it possible to implement behavior where an ajax request refreshes part of a page.
As far as I can tell, in order to render a ViewComponent (and nothing more) from a controller, you have to create a view that simply has:
MyViewComponent.cshtml
Would love to instead be able to do away with the view, and return this directly from my controller, like:
HomeController.cs
The text was updated successfully, but these errors were encountered: