Why is my HttpPost ActionResult not getting reached?

I'm working with two Views that are almost identical; their submit button click handling code is identical.

They both have, in their Controllers (which happen to live in the same file with each other), their own [HttpGet] and [HttpPost] methods. I have a breakpoint on the first line of each Http method:

public ActionResult TLISReport()
{
    var model = new TLISReportModel(); // <-- breakpoint on this line
    . . .

[HttpPost]
public ActionResult TLISReport(TLISReportModel model)
{
    SetUpTLISCombos(); // <-- breakpoint on this line
    . . .

public ActionResult ReceiptCriteria()
{
    var model = new TLDSalesReceiptCriteriaModel(); // <-- breakpoint on this line
    . . .

[HttpPost]
public ActionResult ReceiptCriteria(TLDSalesReceiptCriteriaModel model) 
{
    if (ModelState.IsValid) // <-- breakpoint on this line
    . . .

In the case of "ReceiptCriteria", the default HttpGet method is being reached both when I first navigate to the page and when I select the "Submit" button, but the HttpPost is also reached after that; in the case of "TLISReport", though, only the undecorated (HttpGet) ActionResult is reached - the breakpoint for HttpPost for TLISReport is never reached.

Why would that be?

UPDATE

To answer WannaCSharp's question, both the HTML:

<button type="submit" id="submit_button" class="bottomButtonEnabled">View Report</button>

...and the click event handler are exactly the same for both Views:

$("#submit_button").click(function () {
 . . .

UPDATE 2

Running the app/site, those four methods are reached in this order:

0) [HttpGet] public ActionResult ReceiptCriteria()

After I mash the submit button on the Receipt Report Criteria page:

1) [HttpGet] public ActionResult ReceiptCriteria() - followed by:

2) [HttpPost] public ActionResult ReceiptCriteria(TLDSalesReceiptCriteriaModel model)

The same exact thing (now, since I explicitly added the "[HttpGet]" decoration, takes place with TLISReport: Selecting that report invokes

3) [HttpGet] public ActionResult TLISReport()

After I mash the submit button on the TLIS Report Criteria page:

4) [HttpGet] public ActionResult TLISReport()

5) [HttpPost] public ActionResult TLISReport(TLISReportModel model)

So why is a post not just posting but also getting? That seems like a waste of energy for the gerbils powering the machine.

UPDATE 3

In response to Dismissile's request, here is the Submit code in its entirety, no-holds-barred:

$("#submit_button").click(function () {
    var begD = $('#BeginDateTime').val();
    var endD = $('#EndDateTime').val();
    if (begD > endD) {
        alert('Begin date must be before End date');
        $('#BeginDateTime').focus();
        return false;
    }

    $("#NumberOfResults").css("visibility", "visible");
    $("#NumberOfResults").html("Please wait...");

    EnableButton("submit_button", false);

    // If all are selected, don't enumerate them; just set it at "All" (change of case shows that the logic did execute)
    var deptsList = $('#depts').checkedBoxes();
    if (deptsList.length < deptsArray.length) {
        $('#deptHeader span').html(deptsList.join(", "));
    }
    else if (deptsList.length == deptsArray.length) {
        $('#deptHeader span').html("All");
    }
    // " "
    var sitesList = $('#sites').checkedBoxes();
    $('#sitesHeader span').html(sitesList.join(", "));
    if (sitesList.length < sitesArray.length) {
        $('#sitesHeader span').html(sitesList.join(", "));
    }
    else if (sitesList.length == sitesArray.length) {
        $('#sitesHeader span').html("All");
    }

    $('#hiddenDepts').val(deptsList);
    $('#hiddenSites').val(sitesList);

    var resultsText = jQuery.trim($("#spanNumberOfResults").text());
    if (resultsText != "") {
        $("#NumberOfResults").css("visibility", "visible");

        if (resultsText == "0") {
            $("#NumberOfResults").css("color", "red");
        } else {
            var href = '/@ConfigurationManager.AppSettings["ThisApp"]/TLDCriteria/LoadReport';
            var report_parms = {
                GUID: "@Model.GUID",
                SerialNumber: "@Model.SerialNumber",
                ReportName: "@Model.ReportName"
            };
            window.open(href, "report_window", "resizable=1, width=850, left=" + (screen.width / 2 - 425));
        }
    }
    console.log('made it to the end of submit button click');
}); // end of submit button click

UPDATE 4

More info in response to dismissile's missive:

0) From TLDCriteriaController.cs:

public ActionResult LoadReport()
{
    return View();
}

1) LoadReport.cshtml:

@{
    Layout = null;
}

<!DOCTYPE html>
<html>
    <head runat="server">
        <title>Preview</title>
        <style type="text/css">
            html, body {
                height: 100%;
                overflow: auto;
                width: 100%;
            }

            body {
                margin: 0;
                padding: 0;
            }

            #silverlightControlHost {
                height: 100%;
                text-align: center;
                width: 100%;
            }
        </style>
    </head>
    <body>
        <form id="form1" runat="server" style="height: 100%">
            <div id="silverlightControlHost">
                <object data="data:application/x-silverlight-2," type="application/x-silverlight-2"
                        width="100%" height="100%">
                    <param name="source" value="@Url.Content("~/ClientBin/TLDReporter-SL.xap")" />
                    <param name="onError" value="onSilverlightError" />
                    <param name="background" value="white" />
                    <param name="minRuntimeVersion" value="4.0.60310.0" />
                    <param name="autoUpgrade" value="true" />
                    <a href="http://go.microsoft.com/fwlink/?LinkID=149156&v=4.0.60310.0" style="text-decoration: none">
                        <img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight"
                             style="border-style: none" />
                    </a>
                </object>
                <iframe id="_sl_historyFrame" style="border: 0; height: 0; visibility: hidden; width: 0;"></iframe>
            </div>
        </form>

        <script src="@Url.Content("~/scripts/handle_silverlight_error-1.0.0.js")" type="text/javascript"> </script>
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"> </script>
        <script src="@Url.Content("http://code.jquery.com/jquery-migrate-1.2.1.min.js")" type="text/javascript"> </script>
        <script src="@Url.Content("http://code.jquery.com/ui/1.9.2/jquery-ui.js")" type="text/javascript" ></script>

        <script type="text/javascript">
            function get_user_name() {
                return "@User.Identity.Name";
            }

            function get_xml_data() {
                return window.opener.xml_data;
            }

            function get_receipt_parms() {
                return window.opener.receipt_parms;
            }

            function get_report_parms() {
                return window.opener.report_parms;
            }
        </script>
    </body>
</html>

Upvotes: 0

Views: 4554

Answers (1)

Dismissile
Dismissile

Reputation: 33071

Try putting [HttpGet] on the action results that do not have [HttpPost]. It looks like it can't figure out which one to use.

Upvotes: 3

Related Questions