简体   繁体   English

在ASP.NET MVC中通过Javascript / Jquery导出Excel

[英]Export Excel by Javascript/Jquery in asp.net mvc

View: 视图:

<button onclick="ExportExcell()" type="button">Export to Excell</button>

<div class="panel-body">
    <div class="col-sm-12">
            @Html.Raw(ViewData["html"].ToString());
    </div>
</div>

Javascript/Jquery: Javascript / Jquery:

function ExportExcell() {

window.location.href = "Report/ExportLevyToExcell?="
+ @Html.Raw(new JavaScriptSerializer().Serialize(ViewData["html"]));


 }

Controller: 控制器:

    [HttpPost]
    public void ExportLevyToExcell()
    {
        var result = ViewData["html"];
        //Process....
    }

I tried to post ViewData["html"] to ExportLevyToExcell in controller.However i am seeing syntax error for javascript code.How can i post Viewdata by using javascript jquery thanks. 我试图在控制器中将ViewData [“ html”]发布到ExportLevyToExcell。但是,我看到javascript代码的语法错误。如何使用javascript jquery感谢我发布Viewdata。

Any help will be appreciated. 任何帮助将不胜感激。

I am not sure what are your trying to achieve here, I see several issue in your code. 我不确定您要在这里实现什么,我在您的代码中看到了几个问题。

I tried to post ViewData["html"] to ExportLevyToExcell in controller. 我试图在控制器中将ViewData [“ html”]发布到ExportLevyToExcell。

However I am seeing syntax error for javascript code. 但是,我看到JavaScript代码的语法错误。 How can I post Viewdata by using javascript jquery. 如何使用javascript jquery发布Viewdata。

You don't read values out of ViewData in controller. 您不会从控制器的ViewData中读取值。 These structures are for controller to share some little data with view (ideally view should get data through model ie strongly typed view). 这些结构用于控制器与视图共享一些少量数据(理想情况下,视图应通过模型(即强类型视图)获取数据)。

You can send data from view to controller in QueryString or FormBody. 您可以将数据从视图发送到QueryString或FormBody中的控制器。 As an example, If you write controller like this: 例如,如果您这样编写控制器:

public ActionResult ExportLevyToExcell(string data)
{
    var result = data;

    return HttpNotFound();
}

and change your JS code a bit like this: 然后像这样更改您的JS代码:

function ExportExcell() {

    var data = '@Html.Raw(new JavaScriptSerializer().Serialize(ViewData["html"]))';

    window.location.href = "Home/ExportLevyToExcell?data=" + data;
}

When you click on button, you will see controller receive the value of 当您单击按钮时,您将看到控制器收到的值

ViewData["html"] ViewData [“ html”]

I wish to be of more help however not sure what exactly you want to achieve. 我希望能获得更多帮助,但是不确定您要实现什么目标。

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

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