Skip to content
This repository was archived by the owner on Dec 14, 2018. It is now read-only.

Should be able to return ViewComponents from controller actions (aka ViewComponent ActionResult helper) #2338

Closed
jchadwick opened this issue Apr 4, 2015 · 8 comments

Comments

@jchadwick
Copy link

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

@Component("MyViewComponent")

Would love to instead be able to do away with the view, and return this directly from my controller, like:

HomeController.cs

public IActionResult MyViewComponent()
{
    return Component("MyViewComponent");
}
@jchadwick jchadwick changed the title Should be able to return ViewComponents from controller actions (aka ViewComponent ActionResult) Should be able to return ViewComponents from controller actions (aka ViewComponent ActionResult helper) Apr 4, 2015
@davidfowl
Copy link
Member

Why not use a partial?

@jchadwick
Copy link
Author

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. :)

@yishaigalatzer
Copy link
Contributor

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.

@jchadwick
Copy link
Author

@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?

@yishaigalatzer
Copy link
Contributor

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.

@toddtsic
Copy link

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")

@rynowak
Copy link
Member

rynowak commented Jul 22, 2015

Is it possible to re-invoke a view component?

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:
1). Write your VC
2). Create a controller/action that returns a ViewComponentResult
3). Write some client-side javascript to do an ajax request to your new action and insert the updated content on the page.

I am conceiving the details view as a view component (as I would a user control in webforms)

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.

rynowak added a commit that referenced this issue Jul 22, 2015
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.
rynowak added a commit that referenced this issue Jul 22, 2015
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.
@rynowak rynowak modified the milestones: 6.0.0-beta7, Backlog Jul 23, 2015
@rynowak rynowak self-assigned this Jul 23, 2015
@rynowak
Copy link
Member

rynowak commented Jul 24, 2015

e91ce45

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

6 participants