将参数从 jsonresult 传递给 actionresult

Pass parameters to actionresult from jsonresult

我编写了代码来过滤如下图所示的结果,

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

这是查看文件

@model project_name.Models.SearchVM
....
@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    ....
    <div class="row">
        <div class="col-xs-6">
            <div class="form-group">
                @Html.LabelFor(m => m.Type, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.DropDownListFor(m => m.Type, Model.TypeList, "Select the type", new { @class = "form-control" })
                    @Html.ValidationMessageFor(model => model.Type, "", new { @class = "text-danger" })
                </div>
            </div>
        </div>
    </div>
    ...............
    <div class="row">
        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="button" value="Generate Report" class="btn btn-success submit" onclick="location.href='@Url.Action("ReportExport", "Home", new { type = Model.Type , ............. })'" /> &nbsp; <button id="search" type="button" class="btn btn-success submit">Search</button>
            </div>                      
        </div>
    </div>
}
<table class="table">
    <thead>
        <tr>
            <th>ID</th>
            <th>Product name</th>
            <th>Type</th>
            .........
            <th>Action</th>
        </tr>
    </thead>
    <tbody id="table"></tbody>
</table>

<table id="template" class="table" style="display: none;">
    <tr>
        <td></td>
        <td></td>
        <td></td>
        ........
        <td><a>Edit</a></td>
    </tr>
</table>   

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
    @Scripts.Render("~/bundles/jqueryui")

    <script type="text/javascript">
        $(function () {
            $('.datepicker').datepicker({
                dateFormat: 'yy/mm/dd', changeMonth: true,
                changeYear: true, yearRange: '1910:2015'
            });
        });
    </script>

    <script type="text/javascript">

        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'));
                });
            });
        });
    </script>


}

我想在单击 生成报告 按钮后调用并向 ReportExport 方法发送参数

但是我得到的是空值,我认为这是因为我正在使用 Json 进行搜索,所以我怎样才能得到 Type 值并发送作为参数,

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

您的 'Generate Report' 按钮包含 @Url.Action("ReportExport", "Home", new { type = Model.Type, ...,这是剃须刀代码。 Razor 代码在发送到视图之前在服务器上进行解析,因此它会根据模型的初始值而不是编辑后的值生成路由值。

您可以使用jQuery构建您基于表单控件的url。

Html

<input type="button" id="report" data-baseurl="@Url.Action("ReportExport", "Home")" value="Generate Report" class="..." />

脚本

$('#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() + ...;