传递参数从jsonresult到的ActionResult [英] Pass parameters to actionresult from jsonresult

查看:113
本文介绍了传递参数从jsonresult到的ActionResult的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了code过滤像下面的图像效果,

在这里输入的形象描述

后,一旦过滤器1要发送下列领域作为参数传递给另一个控制器方法的模型值,我可以调用该方法,一旦我点击生成报告按钮

这是视图文件

  @model project_name.Models.SearchVM
....
@using(Html.BeginForm())
{
    @ Html.AntiForgeryToken()
    @ Html.ValidationSummary(真,新{@class =TEXT-危险})
    ....
    < D​​IV CLASS =行>
        < D​​IV CLASS =COL-XS-6>
            < D​​IV CLASS =表单组>
                @ Html.LabelFor(M = GT; m.Type,htmlAttributes:新{@class =控制标签COL-MD-2})
                < D​​IV CLASS =COL-MD-10>
                    @ Html.DropDownListFor(M = GT; m.Type,Model.TypeList,选择型,全新{@class =表格控})
                    @ Html.ValidationMessageFor(型号=> model.Type,新{@class =TEXT-危险})
                < / DIV>
            < / DIV>
        < / DIV>
    < / DIV>
    ...............
    < D​​IV CLASS =行>
        < D​​IV CLASS =表单组>
            < D​​IV CLASS =COL-MD-偏移2 COL-MD-10>
                <输入类型=按钮值=生成报告级=BTN BTN-成功提交的onclick =location.href ='@ Url.Action(ReportExport,家,新的{type =模型。类型.............})/> &安培; NBSP; <按钮ID =搜索类型=按钮级=BTN BTN-成功提交>搜索和LT; /按钮>
            < / DIV>
        < / DIV>
    < / DIV>
}
<表类=表>
    <&THEAD GT;
        &所述; TR>
            <第i个ID< /第i
            <第i产品名称和LT; /第i
            <第i个类型< /第i
            .........
            <第i个动作< /第i
        < / TR>
    < / THEAD>
    < TBODY ID =表>< / TBODY>
< /表><表ID =模板级=表的风格=显示:无;>
    &所述; TR>
        < TD>< / TD>
        < TD>< / TD>
        < TD>< / TD>
        ........
        < TD>< A>编辑< / A>< / TD>
    < / TR>
< /表>@section脚本{
    @ Scripts.Render(〜/包/ jqueryval)
    @ Scripts.Render(〜/包/ jQueryUI的)    <脚本类型=文/ JavaScript的>
        $(函数(){
            $('。日期选择器)。日期选择器({
                DATEFORMAT:'YY / MM / DD',changeMonth:真实,
                changeYear:真,yearRange:1910年:2015年
            });
        });
    < / SCRIPT>    <脚本类型=文/ JavaScript的>        VAR URL ='@ Url.Action(FetchProducts)';
        VAR editUrl ='@ Url.Action(编辑);
        变种类型= $('#类型);
        ..............        VAR模板= $('#模板');
        无功表= $('#表);
        $('#搜索')。点击(函数(){
            table.empty();
            $ .getJSON(URL,{类型:type.val(),......,功能(数据){
                $。每个(数据,功能(索引,项目){
                    变种克隆= template.clone();
                    VAR细胞= clone.find('TD');
                    cells.eq(0)的.text(item.ID);
                    cells.eq(1)的.text(item.Name);
                    cells.eq(2)的.text(项目。形式);
                    ........................
                    cells.eq(7)的.text(item.Status);
                    VAR HREF =@ Url.Action(编辑)'+'/'+ item.ID;
                    cells.eq(8)。儿童('a')的ATTR('href属性中,href)。
                    table.append(clone.find('TR'));
                });
            });
        });
    < / SCRIPT>
}

我要打电话和发送参数的 ReportExport 方式,一旦我点击生成报告按钮

但我得到空值,我想这是因为我在做使用JSON搜索,所以,我怎样才能获得的键入值和发送的参数,

  [HTTPGET]
公众的ActionResult ReportExport(字符串ID,字符串类型,...........)
{


解决方案

您生成报告按钮,包括 @ Url.Action(ReportExport,家,新的{type = Model.Type ...... 这是剃刀code剃刀code是它发送到视图,以便其基于模型的初始值产生的路径值之前在服务器上解析,不是编辑的值。

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

HTML

 <输入类型=按钮ID =报告数据baseURL时=@ Url.Action(ReportExport,家)VALUE =生成报告类=.../>

剧本

  $('#报告')。点击(函数(){
  //构建URL
  VAR URL = $(本)。数据(baseURL时')+ $('#类型)VAL()+'型=?'。'和;类='+ $('#类别)VAL(。 )+ ......;
  //重定向
  location.href =网址;
});

I wrote code to filter results like following image ,

once after it filter I want to send model values of following field as parameters to another controller method, I can call that method once I click Generate Report button

this is view file

@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>


}

I want to call and send parameters to ReportExport method once I click Generate Report button

But I'm getting null values , I think this is because of I'm doing searching using Json , So How can I get Type value and send that as parameter ,

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

解决方案

Your 'Generate Report' button includes @Url.Action("ReportExport", "Home", new { type = Model.Type, ... which is razor code. Razor code is parsed on the server before its sent to the view so its generating the route values based on the initial values of your model, not the edited values.

You can use jQuery to build you url based on the form controls.

Html

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

Script

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

这篇关于传递参数从jsonresult到的ActionResult的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆