将参数传递给jsonresult的actionresult

我编写代码来过滤结果如下图,

在此处输入图像描述

一旦它过滤后我想将以下字段的模型值作为参数发送到另一个控制器方法,我可以在单击Generate Report按钮后调用该方法

这是视图文件

@model project_name.Models.SearchVM .... @using (Html.BeginForm()) { @Html.AntiForgeryToken() @Html.ValidationSummary(true, "", new { @class = "text-danger" }) .... 
@Html.LabelFor(m => m.Type, htmlAttributes: new { @class = "control-label col-md-2" })
@Html.DropDownListFor(m => m.Type, Model.TypeList, "Select the type", new { @class = "form-control" }) @Html.ValidationMessageFor(model => model.Type, "", new { @class = "text-danger" })
...............
 
} .........
ID Product name TypeAction
........
Edit
@section Scripts { @Scripts.Render("~/bundles/jqueryval") @Scripts.Render("~/bundles/jqueryui") $(function () { $('.datepicker').datepicker({ dateFormat: 'yy/mm/dd', changeMonth: true, changeYear: true, yearRange: '1910:2015' }); }); var url = '@Url.Action("FetchProducts")'; var editUrl = '@Url.Action("Edit")'; var type = $('#Type'); .............. var template = $('#template'); var table = $('#table'); $('#search').click(function () { table.empty(); $.getJSON(url, { type: type.val(), ......, function (data) { $.each(data, function (index, item) { var clone = template.clone(); var cells = clone.find('td'); cells.eq(0).text(item.ID); cells.eq(1).text(item.Name); cells.eq(2).text(item.Type); ........................ cells.eq(7).text(item.Status); var href = '@Url.Action("Edit")' + '/' + item.ID; cells.eq(8).children('a').attr('href', href); table.append(clone.find('tr')); }); }); }); }

单击Generate Report按钮后,我想调用并向ReportExport方法发送参数

但我得到空值,我认为这是因为我正在使用Json进行搜索,所以如何获取Type值并将其作为参数发送,

 [HttpGet] public ActionResult ReportExport(string id, string type, ...........) { 

您的“生成报告”按钮包括@Url.Action("ReportExport", "Home", new { type = Model.Type, ...这是razor代码@Url.Action("ReportExport", "Home", new { type = Model.Type, ...代码在发送到视图之前在服务器上解析,因此它根据模型的初始值生成路径值,而不是编辑的值。

您可以使用jQuery根据表单控件构建您的URL。

HTML

  

脚本

 $('#report').click(function() { // build the url var url = $(this).data('baseurl') + '?type=' + $('#Type').val() + '&category=' + $('#Category').val() + ......; // redirect location.href = url; }); 

@Url.Action()是剃刀代码。 它在发送到视图之前在服务器上进行评估。 因此,您必须像上面引用的那样构建您的url。

  var url = $(this).data('baseurl') + '?type=' + $('#Type').val() + '&category=' + $('#Category').val() + ...;