为什么无法访问我的HttpPost ActionResult

本文关键字:HttpPost ActionResult 我的 访问 为什么 | 更新日期: 2023-09-27 18:28:31

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

它们在各自的控制器(恰好位于同一文件中)中都有自己的[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——从未到达TLISReport的HttpPost的断点。

为什么会这样?

更新

为了回答WannaSharp的问题,HTML:

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

和点击事件处理程序对于两个视图完全相同:

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

更新2

运行应用程序/网站,这四种方法按以下顺序到达:

0)[HttpGet]public ActionResult ReceiptCriteria()

在我将收据报告标准页面上的提交按钮捣碎后:

1) [HttpGet]public ActionResult ReceiptCriteria()-然后是:

2) [HttpPost]public ActionResult ReceiptCriteria(TLDSalesReceiptCriteriaModel模型)

同样的事情(现在,由于我明确添加了"[HttpGet]"装饰,TLISReport也发生了:选择该报告会调用

3) [HttpGet]public ActionResult TLISReport()

我在TLIS报告标准页面上点击提交按钮后:

4) [HttpGet]public ActionResult TLISReport()

  • 然后是:

5) [HttpPost]public ActionResult TLISReport(TLISReportModel模型)

那么,为什么一个帖子不仅仅是发布,还有获得呢?对于为机器提供动力的沙鼠来说,这似乎是在浪费能量。

更新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>

为什么无法访问我的HttpPost ActionResult

尝试将[HttpGet]放在没有[HttpPost]的操作结果上。看起来它不知道该用哪一个。