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 7779161
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T18:34:53+00:00 2026-06-01T18:34:53+00:00

I would like to have my Razor View return a string of the html

  • 0

I would like to have my Razor View return a string of the html it renders, so that the controller would return the rendered html string from the View and not just the view. There is no native method I can find in ASP.NET MVC to do this. Are there any workarounds for this?

By way of illustrative example :

 public ActionResult Index()
        {
            return View().ToString; //View has no ToString() method but this is what I am trying to do
        }
  • 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-01T18:34:54+00:00Added an answer on June 1, 2026 at 6:34 pm

    You can use Html.Partial to return an MvcHtmlString of the view.

    You can find the Partial method in System.Web.Mvc.Html.PartialExtensions.

    There’s more information here about this method: http://msdn.microsoft.com/en-us/library/ee402898.aspx

    I personally use this for my RenderPartials (plural) extension method:

       public static void RenderPartials<T>(this HtmlHelper helper, 
                string partialViewName, IEnumerable<T> models, string htmlFormat)
            {
                if (models == null)
                    return;
    
                foreach (var view in models.Select(model => 
                      helper.Partial(partialViewName,model, helper.ViewData)))
                {
                    helper.ViewContext.HttpContext.Response
                      .Output.Write(htmlFormat, view);
                }
            }
    

    Update

    To render your view to a string in your controller you can do something like this (although it seems like a bit of a hack):

    var htmlHelper = new HtmlHelper(new ViewContext(), new ViewPage());
    var viewString = htmlHelper.Partial("PartialViewName");
    

    The reason I say it’s a bit of a hack is because HtmlHelper is designed to be used in your views and not in your controllers or models. That being said, if it works and there isn’t an alternative to stringify a parsed view it might be of use to you.

    Given the amendment to your question you’d be looking for something like this:

    public string Index()
    {
        var htmlHelper = new HtmlHelper(new ViewContext(), new ViewPage());
        return htmlHelper.Partial("PartialViewName");
    }
    

    If the above code doesn’t create the htmlHelper correctly you can create it like this instead:

    TextWriter writer = new StringWriter();
    var htmlHelper = new HtmlHelper(new ViewContext(ControllerContext, 
                    new RazorView(ControllerContext, "","",true,null), 
                    new ViewDataDictionary(), 
                    new TempDataDictionary(), writer), new ViewPage());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to know how can I pass data from controller to view
I would like to generate emails using the Razor view engine. From what I've
I have a problem that I would like have solved via a SQL query.
So in my project i would like have a nice treeview that has images.
I would like to have it so that when I play one Vimeo video
I would like to have an interface that allows for a generic type public
I have 9 ImageViews that when onClick each open a colorpicker, I would like
MVC3 VB.NET Razor view Application... I have a view that uses a for each
I would like to study the source of the Razor view engine. Since MS
I have values in a resx file that I would like to output in

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.