Search code examples
c#.netjqueryasp.net-mvc-2export-to-excel

Render view on backend and execute client side scripts


So I am exporting to excel. The way I do this is I render views on the backend, and then write an excel file using the string of the HTML that the view generated. Problem is, this one view I want to generate is just some blank tables/columns until the client side code executes. It's being rendered, but none of the Jquery is executing so nothing is populated when I export, and I get a blank export. Here is the view:

    <table id="exposureTableBTS">
        <tr>
            <td id="headerBTS" />
        </tr>
        <tr>
            <td id="tableBTS" />
            <td id="graphBTS" />
        </tr>
    </table>

<script>
    $(document).ready(function () {

        $("#exposureTableBTS").bubble({ width: 400, title: 'Exposure:', backgroundColor: '#ffffff'  });

        $.ajax({
                type: "POST",
                url: "/Extranet/mvc/Indications.cfc/GetExposure ",
                dataType: "json",
                data: GetJSONForID(),
                success: function(data) {
                      CreateTableFromPoints(data.expsosure);
                }
            });


    function CreateTableFromPoints(data)
    {
        $("#tableBTS").html(CreateExposureBodyTable(data));
        $("#headerBTS").html(CreateExposureHeaderTable(data));
    }

    function CreateExposureGraph() {
             $.ajax({
                url: "/Extranet/mvc/Indications.cfc/ExposureGraph",
                data: GetJSONForID(),
                error:function(x,e){
                    if(x.status==0){
                        alert('You are offline!!\n Please Check Your Network.');
                    }else if(x.status==404){
                        alert('Requested URL not found.');
                    }else if(x.status==500){
                        alert('Internel Server Error.');
                    }else if(e=='parsererror'){
                        alert('Error.\nParsing JSON Request failed.');
                    }else if(e=='timeout'){
                        alert('Request Time out.');
                    }else {
                        alert('Unknow Error.\n'+x.responseText);
                    }
                },
                success:function(data)
                {
                    $("#graphBTS").html(data);
                }
            });
        }

});
</script>

Here is my backend code:

protected string RenderViewToString()
        {
            using (var writer = new StringWriter())
            {
                var view = new WebFormView(_viewPath);
                var vdd = new ViewDataDictionary<Model>(_model);
                var viewCxt = new ViewContext(_context, view, vdd, new TempDataDictionary(), writer);
                viewCxt.View.Render(viewCxt, writer);
                return writer.ToString();
            }
        }

For pages that get rendered without using Javascript to render or update anything, this works fine. But how do I get this page to render to string AFTER the Javascript is executed?

Thanks!


Solution

  • $(document).ready() {
        $("body").hide();
    })
    

    And then...

    function CreateExposureGraph() {
        $.ajax({
            ...
            complete: function() {
                $("body").show();
            }
        });
    
    }
    

    Just an idea.