传递一个JavaScript变量作为参数传递给@ url.Action() [英] Pass a javascript variable as parameter to @url.Action()

查看:1501
本文介绍了传递一个JavaScript变量作为参数传递给@ url.Action()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能通过一个javascript变量作为参数传递给@ url.Action(),因为据我知道有可能是服务器和客户端的问题,我的要求是我根据过滤器来下载文件,和Ajax调用不列入与下载文件的工作。所以我有哈日code中的@ url.Action()的作品,但能不能够实现这一点,任何人都可以建议我如何通过参数@ url.Action()或任何其他的办法。

这是我的code

 < A HREF =@ Url.Action(导出,新{SelectedAccountType =1,寄件者=2014年2月2日,TODATE =2014- 02-02,SelectedAccount =,SelectedUser =,SelectedTeam =})类=BTN-主ID =exportbutton2>导出为CSV< / A>

这是@ Url.Action

我要分配的参数

 <脚本类型=文/ JavaScript的>
。VAR ACCOUNTTYPE = $('#SelectedAccountType)VAL();
        。VAR寄件者= $('#寄件者)VAL();
        。VAR TODATE = $('#TODATE)VAL();
        。VAR帐户ID = $('#SelectedAccount)VAL();
        VAR用户id = $('#SelectedUser)VAL()。
        。VAR teamId = $('#SelectedTeam)VAL();
< / SCRIPT>


解决方案

您需要建立您的网址使用JavaScript / jQuery的。在视图改变链接

 <一个ID =出口HREF =#>导出为CSV< / A>

然后在剧本

  VAR baseURL时='@ Url.Action(导出)';
$('#出口')。点击(函数(){
  VAR URL = baseURL时+ + $('#SelectedAccountType)VAL()+'&放大器;寄件者=''SelectedAccountType =?'+ $('#寄件者)VAL()+'&放大器; TODATE ='+ $ ('#ToDate')。VAL()+ ...等
  location.href =网址;
});

不过,如果你的表单上都标有 FormMethod.Get ,那么你可以使用普通的提交按钮,不需要的jQuery

  @using(Html.BeginForm(导出,yourControllerName,FormMethod.Get))
{
  @ Html.TextBoxForm(M = GT; m.SelectedAccountType)
  ....
  <输入类型=提交值=导出/>
}

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.

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

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

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

这篇关于传递一个JavaScript变量作为参数传递给@ url.Action()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆