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

后端 未结 1 1938
感情败类
感情败类 2020-11-28 16:30

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

相关标签:
1条回答
  • 2020-11-28 17:10

    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" />
    }
    
    0 讨论(0)
提交回复
热议问题