【问题标题】:Calling a different action in MVC3 using Jquery使用 Jquery 在 MVC3 中调用不同的操作
【发布时间】:2013-09-11 07:49:31
【问题描述】:

我的应用程序是用 C#.Net 编码的 Asp.net MVC3,我的要求是,有一个保存到草稿按钮,可以将数据保存到某个表我的数据库。我在视图上有一个打印按钮。我正在使用 SSRS 打印我的数据。当用户填写表格并单击保存草稿时,该数据将被保存,然后用户可以单击“打印”按钮以获取已保存数据的 SSRS 报告。我的问题是,当用户单击打印按钮时,应该会打开一个新选项卡,在该选项卡中应该可以看到报告。

下面是我的查看代码

    $("#Print_button").live("click", function () {
       $('#My_Form').attr('action', 'My Controller action which passes parameter to 
       SSRS report');
    });

我的打印按钮

的HTML
 <input type="submit" id="Print_button"  value="Print"  />

控制器代码

     [HttpPost]
     public ActionResult Method_That_Pass_ParameterTo_SSRS(MyClass Class_Object)
     {
          My Logic to pass the parameters to SSRS report
          return View(Class_Object);
     }

我解决问题的尝试

1.我尝试在按钮 HTML 中添加 Target="_blank"

2. jquery Print_Button 点击​​事件中的 window.open('MyAction','_blank')

【问题讨论】:

    标签: jquery html asp.net-mvc-3 razor


    【解决方案1】:

    试试这个。 由于正在尝试将数据打印为新窗口/选项卡中的报告。

    将您的按钮类型更改为按钮并定义这样的点击。

    <input type="button" id="Print_button"  value="Print" onclick="ShowReport()"/>
    

    然后在您的操作方法方法中。

     public ActionResult Method_That_Pass_ParameterTo_SSRS(MyClass Class_Object)
     {
          My Logic to pass the parameters to SSRS report
          return View(Class_Object);
     }
    

    然后进入你的脚本

    <script type="text/javascript">
        function ShowReport(e) {
            // you can use dataitem here if needed to pass any variable
            //var dataItem = this.dataItem($(e.currentTarget).closest("tr"));            
            //BaseUrl will the url of your website
            // for example if url is http://localhost:10259/Home/Index
            //then BaseUrl is "http://localhost:10259/"
            var FullUrl = "Baseurl"+ "ControllerName/ActionName/" + param1(ifany);
            window.open(
                     'FullUrl',
                     '_blank' // <- This is what makes it open in a new window.
                     );
        }        
    </script>
    

    只需确保您的控制器将数据正确传递给所需的视图即可。

    【讨论】:

      【解决方案2】:

      我会稍微改变一下逻辑。如果数据已经通过点击其他按钮保存,则不能使用表单传递参数。

      相反,您可能拥有Html.ActionLink 并在单击后替换参数。使用这种方法,您可以在新选项卡中打开结果。看:

      @Html.ActionLink("Print", "Action", "Controller", new{param = xxx}, new {@target="_blank", @class="print"})
      
          <script>
          $(function () {
          $('.print').click(function () {          
                      var param = $(".somediv").val() //*Get your param value
                      this.href = this.href.replace("xxx", param);
                  });
          });
          </script>
      

      【讨论】:

        猜你喜欢
        • 2012-06-25
        • 1970-01-01
        • 1970-01-01
        • 2012-06-26
        • 2012-09-11
        • 2013-05-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多