将 javascript 变量作为参数传递给 @url.Action() [英] Pass a javascript variable as parameter to @url.Action()
问题描述
是否可以将javascript变量作为参数传递给@url.Action(),因为据我所知可能存在服务器和客户端问题,我的要求是我必须根据过滤器下载文件,和 ajax 调用不适用于下载文件.所以我对有效的@url.Action() 进行了编码,但无法实现这一点,谁能建议我如何将参数传递给 @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. 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.
这是我的代码
<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>
这是我想分配给@Url.Action的参数
and this is the parameter i want to assign to @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>
推荐答案
您需要使用 javascript/jquery 构建您的 url.在视图中将链接更改为
You need to build you url using javascript/jquery. In the view change the link to
<a id="export" href=#">Export as CSV</a>
然后在脚本中
var baseurl = '@Url.Action("Export")';
$('#export').click(function() {
var url = baseurl + '?SelectedAccountType=' + $('#SelectedAccountType').val() + '&FromDate=' + $('#FromDate').val() + '&ToDate=' + $('#ToDate').val() + ...etc
location.href=url;
});
但是如果你的表单被标记为FormMethod.Get
,那么你就可以使用普通的提交按钮,不需要jquery
However if your form is marked with FormMethod.Get
, then you can just use a normal submit button and no jquery is required
@using (Html.BeginForm("Export", "yourControllerName", FormMethod.Get))
{
@Html.TextBoxForm(m => m.SelectedAccountType)
....
<input type="submit" value="Export" />
}
这篇关于将 javascript 变量作为参数传递给 @url.Action()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!