0

I am working in MVC and I want to convert the view to HTML which includes SVG charts(high chart). I have used following code for the same.

static string RenderViewToString(ControllerContext context,
                                 string viewPath,
                                 object model = null,
                                 bool partial = false)
{
    // first find the ViewEngine for this view
    ViewEngineResult viewEngineResult = null;
    if (partial)
        viewEngineResult = ViewEngines.Engines.FindPartialView( context, viewPath);
    else
        viewEngineResult = ViewEngines.Engines.FindView(context, viewPath, null);

    if (viewEngineResult == null)
        throw new FileNotFoundException("View cannot be found.");

    // get the view and attach the model to view data
    var view = viewEngineResult.View;
    context.Controller.ViewData.Model = model;

    string result = null;

    using (var sw = new StringWriter())
    {
        var ctx = new ViewContext(context, view,
                                  context.Controller.ViewData,
                                  context.Controller.TempData,
                                  sw);
        view.Render(ctx, sw);
        result = sw.ToString();
    }

    return result;
}

From the above code we can convert static view to html but not dynamic. My view has Jquery client scripts which are written in document.ready() to draw default chart and associated tables. Please help me to render the view page after chart and associated data manipulations.

  • Why do you want to render it to a string manually? Why don't you just return a partial view from a controller method that your AJAX can call? See [asp.net MVC partial view controller action](http://stackoverflow.com/questions/1371031/asp-net-mvc-partial-view-controller-action). – mason Mar 02 '15 at 17:43
  • I want to convert the rendered view to Excel. For excel conversion I am using a common conversion class which needs HTML – vjdeveloper Mar 02 '15 at 17:50
  • 2
    So this is probably an [XY Problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). In an application that's following good separation of concerns, your Excel library shouldn't depend on rendering a view. Your conversion library should accept a model object, then generate the Excel document based on that. And I don't know what Excel conversion library you're using, but it's probably not a good approach to generate Excel files from HTML as I explain on my [blog](https://masonmcg.com/blog/entry/properly-generating-excel-files-in-net). – mason Mar 02 '15 at 17:54
  • Thanks for the suggestion. But I am not converting my view to EXCEL as you pointed. I need to extract SVG content from the rendered HTML to provide it as an image in excel file. Also it seems that you are writing response directly as excel where sheet implementation gets more difficult. – vjdeveloper Mar 02 '15 at 18:22
  • If JQuery is already doing the heavy lifting, can you use it to convert the SVG to image instead? eg http://stackoverflow.com/questions/3975499/convert-svg-to-image-jpeg-png-etc-in-the-browser – SteveCav Mar 03 '15 at 00:32
  • You are right. I am planning the same approach to convert SVG to JPG. But before all, we need to invoke document ready function in order to populate high chart which is based on some filter condition. So kindly suggest any way to populate the complete html of rendered view. So that i can find out the SVG file and do the conversion. Hope now my question is clear. Thanks – vjdeveloper Mar 03 '15 at 04:32
  • Any luck on this? I'm looking to render a view that contains javascript to render a chart to HTML and am not having much luck. Thanks! – jay Sep 19 '17 at 14:53

0 Answers0