Is it possible to invoke a View Component from controller and render it to a string? I am really looking for some code example for this. Any help will be much appreciated.
相关问题
- Can I use MvcJsonOptions configured during Startup
- Singleton with AsyncLocal vs Scope Service
- gRPC client do not dispose Channel
- Unable to resolve service for type 'Microsoft.
- Which encryption algorithm do we use in Asp.net 5
相关文章
- EF Core 'another instance is already being tra
- Re-target .NET Core to net471, net 472
- How to replace Middleware in integration tests pro
- Why CsvHelper not reading from MemoryStream?
- How to define function that returns html in asp.ne
- Bad Request - Invalid Hostname when accessing loca
- Should I use xUnitPublisher or xUnitBuilder after
- How to log ASP.NET Core input JSON serialization e
Please refer to example from official ASP.NET article on ViewComponent
In their example, the view component is called directly from the controller as follows:
Here's a tag helper that I created to embed components via HTML like syntax. Invoking from a TagHelper like this should closely match invoking from a Controller.
ViewComponent Tag Helper
Usage
As of beta7 it is now possible to return a ViewComponent directly from a controller. Check the MVC/Razor section of the announcement
So now the code for returning the sample view component just needs to be:
Code from dotnetstep's answer updated for MVC 6.0.0-beta4 (VS2015 RC):
You can do that but you have to apply following thing as It is render by DefaultViewComponentHelper.
You have to create instance of this and to create that you need IViewComponentSelector and IViewComponentInvokerFactory.
To do this I have done following thing.
Here is my sample View Component.