0

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

他们都有,在他们的控制器中(它们碰巧彼此存在于同一个文件中),他们自己的 [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 断点。

为什么会这样?

更新

要回答 WannaCSharp 的问题,两个 HTML:

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

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

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

更新 2

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

0) [HttpGet] 公共 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)加载报告.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>
4

1 回答 1

3

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

于 2013-08-20T17:34:10.003 回答