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

发布于 2024-10-19 17:29:04 字数 2739 浏览 1 评论 0原文

所以我要导出到excel。我这样做的方法是在后端渲染视图,然后使用视图生成的 HTML 字符串编写 Excel 文件。问题是,我想要生成的这一个视图只是一些空白表/列,直到客户端代码执行为止。它正在渲染,但没有任何 Jquery 正在执行,因此导出时没有填充任何内容,并且导出为空白。这是视图:

    <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>

这是我的后端代码:

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();
            }
        }

对于不使用 Javascript 渲染或更新任何内容而渲染的页面,这工作得很好。但是,如何在执行 Javascript 后将此页面呈现为字符串呢?

谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月下客 2024-10-26 17:29:04
$(document).ready() {
    $("body").hide();
})

然后...

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

}

只是一个想法。

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

And then...

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

}

Just an idea.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文