在后端渲染视图并执行客户端脚本
所以我要导出到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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
然后...
只是一个想法。
And then...
Just an idea.