0

I am looking at different posts so that on button click can we do two actions.

My aspx page contains form and on button click it will submit form to controller and I am using form data in my controller and based up on the values in my form I am rendering partial views and I wan tto show my partial view in my aspx .

I am doing below steps :

   <form id="StatsForm" name="StatsForm" action="../Stats/Index/" method="POST"
                enctype="multipart/form-data">
                <%= Html.AntiForgeryToken()%>
                <% Html.RenderPartial("OptionalFields"); %>
            </form>

 <script type="text/javascript" language="javascript">
                //<![CDATA[

                $(document).ready(function () {
                    $("#GetReport").click(function () {
                        $("form[name=StatsForm]").submit();

                    });
                });

                //]]>
            </script>

My controller :

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Index(FormCollection form)
    {
        // Deal with the form 
        var strId= Convert.ToInt32(form["manufacturerId"]);
        var str1Id= Convert.ToInt32(form["reportId"]);
        var str2Id= Convert.ToInt32(form["categoryId"]);
        var str3Id= Convert.ToInt32(form["retailerId"]);
        var str4Id= Convert.ToInt32(form["countryId"]);
        var str5Id= Convert.ToInt32(form["regionId"]);
        var str6Id= (form["ManufacturerWidgetId"]);
        var startDate = new DateTime(1, 1, 1, 0, 0, 0, 0);
        var endDate = new DateTime(1, 1, 1, 0, 0, 0, 0);

        if (!String.IsNullOrEmpty(form["StartDate"]))
        {

           startDate = Convert.ToDateTime(form["StartDate"]);
        }
        if (!String.IsNullOrEmpty(form["EndDate"]))
        {
            endDate = Convert.ToDateTime(form["EndDate"]);
        }

        var reportName = _reportRepository.GetReport(reportId);


        var stats = new Stats
                        {

                            };

         switch (reportName.Code)
        {
            case "ABC":
                return RedirectToAction("InterStats",
                                    new
                                    {


                                    });
                break;
            case "XYZ":
                return RedirectToAction("ParametersCumLeads",
                                    new
                                    {

                                    });
                break;
            case "IMP":

                break;
        }

My partial view:

    [AcceptVerbs(HttpVerbs.Get | HttpVerbs.Post)]
    public ActionResult InterStats(int str1Id, int str2Id, DateTime startDate, DateTime endDate)
    {
        var str = _manufacturerWidgetsRepository.GetManufacturerWidgetByManufacturerAndCountry(manufacturerId, countryId);
        var report = new ABCReport();
        var reportList = new List<ReportList>(); // a list of my anonymous type without the relationships
        report.reportList = new List<Record>();
        var count = 1;
        foreach (var mw in str)
        {
           // I am doing some function



            //  Create the data for the report
                       }
                return PartialView(report);
    }

And I am trying to display my partial view in my aspx. My question is without clicking my submit button twice how can I submit form to my controller and also inject my partial views in my aspx.

1 Answer 1

1

Ok,

there are 2 things that you need to do to achieve this:

  1. You need your controller to return a 'PartialView' (perhaps even a master PartialView that delegates out to the partials that you call in InterStats), rather than doing a RedirectToAction
  2. Your .submit() action needs to be refactored to submit the form via ajax and then accept the returned ActionResult in the success callback and append that onto your page.

I'm in transit, otherwise would make a quick example. Will update later if still required.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.