0

I have a requirement where I need HTML data from View to be saved in a string variable, something like this

string data = GetData(Id)

and GetData is a method which returns View like

public ActionResult GetData(int Id) { return PartialView("ViewName"); }

My requirement is to get whatever this View creates/return as an HTML to be assigned to string variable data

Is it actually possible?

Thanks for Help

tereško
  • 58,060
  • 25
  • 98
  • 150
Jay
  • 1,037
  • 5
  • 23
  • 41

3 Answers3

1

Try this

 public static string GetTemplateContentInstance(ControllerContext controllerContext, string viewName, object model)
    {
        if (string.IsNullOrWhiteSpace(viewName))
            //Return name of view for current action
            viewName = controllerContext.RouteData.GetRequiredString("action");

        controllerContext.Controller.ViewData.Model = model;
        using (StringWriter stringWriter = new StringWriter())
        {
            ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(controllerContext, viewName);
            ViewContext viewContext = new ViewContext(controllerContext, viewResult.View, controllerContext.Controller.ViewData, controllerContext.Controller.TempData, stringWriter);
            viewResult.View.Render(viewContext, stringWriter);

            return stringWriter.GetStringBuilder().ToString();
        }
    }

Use as below

 string viewBody = GetTemplateContentInstance(this.ControllerContext, Url.Content("ViewName"), ModelName)
Amit
  • 15,217
  • 8
  • 46
  • 68
  • Amit: just to calrify, what above code suggest is i add my data in a ````Model```` and then pass that ````Model```` as parameter? Is it what ModelName is? – Jay May 15 '13 at 15:06
  • this is your modelname that render your view else pass as null – Amit May 15 '13 at 15:06
0

Try this:

private string GetHtmlFromUrl(string url)
{
    string html = "";
    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);

    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    // get the response stream.
    //Stream responseStream = response.GetResponseStream();
    // use a stream reader that understands UTF8
    StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
    html = reader.ReadToEnd();
    // close the reader
    reader.Close();
    response.Close();
    return html;//return content html
}

Usage:

string data = GetHtmlFromURL("/YourController/GetData/{ID}") 
//or GetHtmlFromURL("/YourController/GetData?ID={ID}") if your route isn't set to accept ID
Kaizen Programmer
  • 3,798
  • 1
  • 14
  • 30
  • Thanks Michael for quick response, I have copied your code but get this error ````The name 'GetHtmlFromURL' does not exist in the current context```` . I have copied over ````GetHtmlFromURL```` method in same class but cant see why should get this error. Any idea? – Jay May 15 '13 at 14:16
  • I'd need to see more of your code to tell you. Update your question with what you have currently. – Kaizen Programmer May 15 '13 at 15:06
0

You try to create an instance of an HtmlHelper and use the RenderPartial extension methods within that helper

Note: you need to include a using to System.Web.Mvc.Html to access the RenderPartial

public static HtmlHelper GetHtmlHelper( this Controller controller )
        {
            var viewContext = new ViewContext( controller.ControllerContext, new FakeView(), controller.ViewData, controller.TempData, TextWriter.Null );
            new HtmlHelper( viewContext, new ViewPage() ).RenderPartial("/mypartial");
        }

        public class FakeView : IView
        {
            public void Render( ViewContext viewContext, TextWriter writer )
            {
                throw new InvalidOperationException();
            }
        }
miskiw
  • 94
  • 6