简体   繁体   English

在后端渲染视图并执行客户端脚本

[英]Render view on backend and execute client side scripts

So I am exporting to excel. 所以我出口到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. 我这样做的方法是在后端渲染视图,然后使用视图生成的HTML字符串编写一个excel文件。 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. 它正在被渲染,但是没有一个Jquery在执行,所以导出时没有填充任何内容,并且导出为空。 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. 对于在不使用Javascript呈现或更新任何内容的情况下呈现的页面,这可以正常工作。 But how do I get this page to render to string AFTER the Javascript is executed? 但是,如何在执行Javascript后使此页面呈现为字符串?

Thanks! 谢谢!

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

And then... 接着...

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

}

Just an idea. 只是一个主意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM