简体   繁体   English

将JavaScript变量作为参数传递给@ url.Action()

[英]Pass a javascript variable as parameter to @url.Action()

is it possible to pass a javascript variable as a parameter to @url.Action(), because as far as i know there may be server and client side issue, my requirement is i have to download the file according to filter, and ajax call doesnot work with downloading the file. 是否有可能将javascript变量作为参数传递给@ url.Action(),因为据我所知可能存在服务器和客户端问题,我的要求是我必须根据过滤器下载文件,并进行ajax调用不适用于下载文件。 so i have harcode the @url.Action() that works but can not able to implement this, can anyone suggest me how to pass the parameter to @url.Action() or any other approach. 所以我已经对@ url.Action()进行了编码,但无法实现,有人可以建议我如何将参数传递给@ url.Action()或任何其他方法。

here is my code 这是我的代码

<a href="@Url.Action("Export", new { SelectedAccountType="1", FromDate = "2014-02-02", ToDate = "2014-02-02", SelectedAccount = "", SelectedUser = "", SelectedTeam = "" })" class="btn-primary" id="exportbutton2"> Export as CSV</a>

and this is the parameter i want to assign to @Url.Action 这是我想分配给@ Url.Action的参数

<script type="text/javascript">
var accountType = $('#SelectedAccountType').val();
        var fromDate = $('#FromDate').val();
        var toDate = $('#ToDate').val();
        var accountId = $('#SelectedAccount').val();
        var userId = $('#SelectedUser').val();
        var teamId = $('#SelectedTeam').val();
</script>

You need to build you url using javascript/jquery. 您需要使用javascript / jquery构建网址。 In the view change the link to 在视图中,将链接更改为

<a id="export" href=#">Export as CSV</a>

Then in the script 然后在脚本中

var baseurl = '@Url.Action("Export")';
$('#export').click(function() {
  var url = baseurl + '?SelectedAccountType=' + $('#SelectedAccountType').val() + '&FromDate=' + $('#FromDate').val() + '&ToDate=' + $('#ToDate').val() + ...etc
  location.href=url;
});

However if your form is marked with FormMethod.Get , then you can just use a normal submit button and no jquery is required 但是,如果您的表单标记有FormMethod.Get ,那么您可以使用常规的提交按钮,而无需使用jquery

@using (Html.BeginForm("Export", "yourControllerName", FormMethod.Get))
{
  @Html.TextBoxForm(m => m.SelectedAccountType)
  ....
  <input type="submit" value="Export" />
}

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

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