javascript-将参数从jsonresult传递到actionresult
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript-将参数从jsonresult传递到actionresult,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3130字,纯文字阅读大概需要5分钟。
内容图文
我编写了代码来过滤结果,如下图所示,
过滤后,一旦我想将以下字段的模型值作为参数发送给另一个控制器方法,单击生成报告按钮后就可以调用该方法
这是查看文件
@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 , ............. })'" /> <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(“ ReportExport”,“ Home”,new {type = Model.Type,…这是剃刀代码.在将剃刀代码发送到视图之前已在服务器上对其进行了解析,因此它会根据模型的初始值而不是已编辑的值生成路线值.
您可以使用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;
});
内容总结
以上是互联网集市为您收集整理的javascript-将参数从jsonresult传递到actionresult全部内容,希望文章能够帮你解决javascript-将参数从jsonresult传递到actionresult所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。