gpt4 book ai didi

javascript - 将参数从 jsonresult 传递到 actionresult

转载 作者:行者123 更新时间:2023-11-28 19:00:24 29 4
gpt4 key购买 nike

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

enter image description here

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

这是查看文件

@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, ...........)
{

最佳答案

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

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

关于javascript - 将参数从 jsonresult 传递到 actionresult,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32688808/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com