为什么我的HttpPost ActionResult无法到达?

时间:2013-08-20 17:04:30

标签: c# asp.net-mvc visual-studio-2010 http-post

我正在使用两个几乎相同的视图;他们的提交按钮点击处理代码 相同。

他们都在他们的控制器中(碰巧彼此存在于同一个文件中),他们自己的[HttpGet]和[HttpPost]方法。我在每个Http方法的第一行都有一个断点:

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

在“ReceiptCriteria”的情况下,正在被达到缺省HTTPGET方法既当我第一导航到该页面,当我选择“提交”按钮,但HttpPost被后也达到;在“TLISReport”的情况下,虽然,只有在达到未修饰的(HTTPGET)的ActionResult - 用于HttpPost断点用于从未达到TLISReport

为什么会这样?

更新

回答WannaCSharp的问题,HTML:

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

...并且两个视图的click事件处理程序完全相同:

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

更新2

运行app / site,按以下顺序访问这四种方法:

0) [HttpGet] public ActionResult ReceiptCriteria()

在“收据报告标准”页面上混合提交按钮后:

1) [HttpGet] public ActionResult ReceiptCriteria() - 接着是:

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

同样的事情(现在,因为我明确地添加了“[HttpGet]”装饰,发生在TLISReport: 选择该报告会调用

3) [HttpGet] public ActionResult TLISReport()

在PLIS报告标准页面上混合提交按钮后:

4) [HttpGet] public ActionResult TLISReport()

  • 接着是:

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

那么,为什么帖子不仅仅是张贴而且还要收到?对于驱动机器的沙鼠而言,这似乎是浪费精力。

更新3

在回应Dismissile的请求时,这里是完整的提交代码,无限制:

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

更新4

更多信息以回应解雇的信件:

0)来自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>

1 个答案:

答案 0 :(得分:3)

尝试将[HttpGet]放在没有[HttpPost]的操作结果上。看起来它无法弄清楚使用哪一个。