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

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="https://stackoverflow.com/questions/30731582/@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" />
}

Leave a Comment