Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8307547
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T18:32:55+00:00 2026-06-08T18:32:55+00:00

I would like to render a PartialView to an HTML string so I can

  • 0

I would like to render a PartialView to an HTML string so I can return it to a SignalR ajax request.

Something like:

SignalR Hub (mySignalHub.cs)

public class mySignalRHub: Hub
{
    public string getTableHTML()
    {
        return PartialView("_MyTablePartialView", GetDataItems()) // *How is it possible to do this*
    }
}

Razor PartialView (_MyTablePartialView.cshtml)

@model IEnumerable<DataItem>

<table>
    <tbody>
        @foreach (var dataItem in Model)
        {
        <tr>
            <td>@dataItem.Value1</td>
            <td>@dataItem.Value2</td>
        </tr>
        }
    </tbody>
</table>

HTML (MySignalRWebPage.html)

<Script>
    ...      
    //Get HTML from SignalR function call
    var tableHtml = $.connection.mySignalRHub.getTableHTML();

    //Inject into div
    $('#tableContainer).html(tableHtml);
</Script>

<div id="tableContainer"></div>

My problem is that I can’t seem to render a PartialView outside of a Controller. Is it even possible to render a PartialView outside of a Controller? It would be very nice to still be able to leverage the awesome HTML generating abilities that come with Razor.

Am I going about this all wrong? Is there another way?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T18:32:56+00:00Added an answer on June 8, 2026 at 6:32 pm

    Here, this is what I use in Controllers for ajax, I modified it a bit so it can be called from method instead of controller, method returnView renders your view and returns HTML string so you can insert it with JS/jQuery into your page when you recive it on client side:

      public static string RenderPartialToString(string view, object model, ControllerContext Context)
            {
                if (string.IsNullOrEmpty(view))
                {
                    view = Context.RouteData.GetRequiredString("action");
                }
    
                ViewDataDictionary ViewData = new ViewDataDictionary();
    
                TempDataDictionary TempData = new TempDataDictionary();
    
                ViewData.Model = model;
    
                using (StringWriter sw = new StringWriter())
                {
                    ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(Context, view);
    
                    ViewContext viewContext = new ViewContext(Context, viewResult.View, ViewData, TempData, sw);
    
                    viewResult.View.Render(viewContext, sw);
    
                    return sw.GetStringBuilder().ToString();
                }
            }
    
            //"Error" should be name of the partial view, I was just testing with partial error view
            //You can put whichever controller you want instead of HomeController it will be the same
            //You can pass model instead of null
            private string returnView()
            {
                var controller = new HomeController();
                controller.ControllerContext = new ControllerContext(HttpContext,new System.Web.Routing.RouteData(), controller);
                return RenderPartialToString("Error", null, new ControllerContext(controller.Request.RequestContext, controller));
            }
    

    I didn’t test it on a Hub but it should work.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to render a list of HTML links in ASP.NET MVC. Note
I would like to render the 3D volume data: Density(can be mapped to Alpha
I would like to render project documentation in html from a lighweight markup language
I'm trying to create something like *.ascxs' factory. Scenario: I would like to render
I would like to render a constructions like: <a href='/home'>Home</a> <span class='active'>Community</span> <a href='/about'>About</a>
I have 2 arrays that I would like to render in a template, one
I would like to re-render a list made using Marionette.ItemView when the corresponding model
I'm using Java3D to render a three-dimensional scene. I would like to overlay a
I want to use libharu to render my documents to PDF. I would like
Can anyone elaborate on why you'd define ViewData[MenuData] on every action for something like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.