Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

Render ViewComponent as string from controller

$
0
0

Hi, I want to render viewcomponent as string in the controller itself. Not in the cshtml.

Here is the code I'm using now but it gets me a null reference exception in the the InvokeAsyncCore inside DefaultViewComponentHelper which implement IViewComponentHelper :

    public sealed class ViewComponentRender : IViewComponentRender
    {
        private readonly IViewComponentHelper _viewComponentHelper;

        public ViewComponentRender(IViewComponentHelper viewComponentHelper)
        {
            _viewComponentHelper = viewComponentHelper;
        }

        #region Implementation of IViewComponentRender

        public async Task<string> RenderToStringAsync(Type componentType, object arguments)
        {
//Exception in the line below : IHtmlContent htmlContent = await _viewComponentHelper.InvokeAsync(componentType, arguments); using (StringWriter stringWriter = new StringWriter()) { htmlContent.WriteTo(stringWriter, HtmlEncoder.Default); return stringWriter.ToString(); } } public async Task<string> RenderToStringAsync(Type componentType) {
//Exception in the line below : IHtmlContent htmlContent = await _viewComponentHelper.InvokeAsync(componentType); using (StringWriter stringWriter = new StringWriter()) { htmlContent.WriteTo(stringWriter, HtmlEncoder.Default); return stringWriter.ToString(); } } #endregion }

Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>