简体   繁体   English

从控制器方法调用文件(aspx.cs文件)后面的代码内部方法

[英]call to method inside code behind file(aspx.cs file) from controller method

I have following controller method 我有以下控制器方法

public class ReportController : Controller
{
    // GET: Report
    public ActionResult Incomplete_Product_Report()
    {
        return View();
    }

}

I want to call following method which is ShowReport() inside the code behind file in aspx web-form . 我想在aspx web-form文件后面的代码内调用以下方法ShowShow()

  private void ShowReport()
  {

  //DataSource
  DataTable dt = GetData(type.Text, category.Text,subsidary.Text,country.Text, dateHERE.Text);
   ....................
  }

  private DataTable GetData(string type, string category, string country, string subsidary, string dateHERE)
  {
    ................
  }

then ShowReport() method call and pass parameters call GetData() 然后调用ShowReport()方法并传递参数调用GetData()

I have following view form to filter results , http://s9.postimg.org/95xvv21z3/wewrwr.png 我有以下视图表单可以过滤结果, http://s9.postimg.org/95xvv21z3/wewrwr.png

also I have following aspx webform to generate report http://s7.postimg.org/iz4zdety3/44r3.png 我也有以下aspx Webform来生成报告http://s7.postimg.org/iz4zdety3/44r3.png

once I click "Generate Report" button in view form I should be able to pass parameters generate results in webform and show Microsoft report wizard(RDLC) like 2nd image . 单击视图表单中的“生成报告”按钮后,我应该能够在Web表单中传递参数生成结果,并像第二幅图像一样显示Microsoft报告向导(RDLC)。

Now I have done these things separately, I want to link those together 现在我已经分别完成了这些事情,我想将它们链接在一起

You asked in the question title about calling method inside code behind file from controller, and it's simple. 您在问题标题中询问有关从控制器在文件后面的代码内部调用方法的问题,这很简单。 Since code behind files are nothing but the class, you can call them just like any other method's class something like this 由于文件背后的代码不过是类,因此可以像调用其他任何方法的类一样调用它们

 public ActionResult ShowForm()
    {
        MvcApplication1.Webforms.Reportform form = new Webforms.Reportform();
        form.ShowForm();
    }

But I don't think you are looking for that. 但是我不认为您正在寻找那个。 As you explain with your images, you want to call the ASPX functionality on the "Generate Report" view that is generated in MVC. 在解释图像时,您想在MVC中生成的“ Generate Report”视图上调用ASPX功能。 You can do that in two ways, either you collect all the necessary parameters on the client side (javascript, jquery etc), and then from there directly redirect to your aspx with query string such as 您可以通过两种方式做到这一点,要么在客户端收集所有必需的参数(javascript,jquery等),然后从那里直接使用查询字符串将其重定向到aspx,例如

$("#generateReport").on('click', function() {

var category = $("#category").val();
//similarly do for all the fields

if (category!= undefined && category != null) {
    window.location = '/Report.aspx?category=' + category;
}
});​

In this case, you will also need to write logic inside the Report.aspx to reach values from query string and then call your showreport method with appropriate inputs. 在这种情况下,您还需要在Report.aspx内编写逻辑以从查询字符串获取值,然后使用适当的输入调用showreport方法。

Another way to do this would be to post the GenerateReport back to MVC, collect parameters and then send it further to aspx, something like this 这样做的另一种方法是将GenerateReport发回到MVC,收集参数,然后将其进一步发送给aspx,类似这样

    [HttpPost]
    public ActionResult GenerateReport(FormCollection collection)
    {
        try
        {
            string category = collection["category"];
            // TODO: Add similar information for other fields

            return Redirect(string.format("/Report.aspx?category={0}",category)); //add additional parameters as required
        }
        catch
        {
            return View();
        }
    }

This would cause an extra return trip though as compared to direct call from client side script. 与从客户端脚本直接调用相比,这将导致额外的回程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从类文件(.cs)文件调用函数到文件(.aspx.cs)之后的另一个代码? - How to call function from class file (.cs) file to another code behind file (.aspx.cs)? 如何在aspx.cs隐藏代码中从静态方法调用非静态方法 - How to call non-static method from static method in an aspx.cs code-behind 如何在 aspx.cs 文件的 App_Code 文件夹中调用 in.cs 文件中的方法? - How to call a method in .cs file in App_Code folder in aspx.cs file? 无法使用更新面板从 aspx 页面触发代码隐藏 (aspx.cs) 方法 - Unable to trigger the code-behind (aspx.cs) method from aspx page using update panel 使用jQuery load()调用.aspx.cs代码后面的函数 - Calling a function in the .aspx.cs code behind file with jQuery load() ListView在文件aspx.cs后面的代码中检索值 - ListView Retrieving Value in Code Behind File aspx.cs 创建代码隐藏文件-aspx.cs - Creating Code-Behind file - aspx.cs 如何在同一项目中从aspx.cs调用方法到另一个aspx.cs? - How to call a method from aspx.cs to another aspx.cs in same project? 如何在文件 .aspx.cs 文件后面的 C# 代码中从 .js 文件访问 javascript 对象 - How to access a javascript object from .js file in C# code behind file .aspx.cs file aspx.cs文件中的Save方法需要哪些参数,以便Ajax可以调用此方法? - What Parameters are required for Save Method in aspx.cs file So that Ajax Can Call This method?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM