简体   繁体   中英

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 .

  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()

I have following view form to filter results , http://s9.postimg.org/95xvv21z3/wewrwr.png

also I have following aspx webform to generate report 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 .

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

$("#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.

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

    [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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM