簡體   English   中英

ASP.NET MVC 3 AJAX請求返回404 Not Found錯誤

[英]ASP.NET MVC 3 AJAX request returns 404 Not Found error

這是我的代碼(以下發現問題):

視圖

// This function is called by another function when radioButtonGroup.change().
var requestValues = function (form) {
    var option = form.find("input:radio:checked").attr("value");

    // This seemingly shows the correct url for the action method desired.
    alert("Form Action: " + form[0].action + "\nForm Method: " + form[0].method);

    if (form.valid()) {
        $.ajax({
            url: form[0].action,
            type: form[0].method,
            data: option,
            success: function (result) {
                alert("Had success.");
                $('#createForm').replaceWith(result);
            },
            error: function (xhr) {
                alert("An error occurred: " + xhr.status + " " + xhr.statusText);
            }
        });
    }
    return false;
}

...(other code here)...

@using (Html.BeginForm("CreateForm", "MyController", FormMethod.Post, 
                        new { @id = "optionForm" }))
{
    <div id="options">
        @foreach (MyOption op in Model.GetOptions()) {
            <div class="editor-field">
            @Html.RadioButton("formOption", op.OptionType, false, 
                new { @id = op.ID, @title = @op.Description })
            <label for="@op.ID">@op.Name</label>
            </div>
        }
    </div>
    <input type="submit" value="Select" style="display:none;" />
}

CONTROLLER

[HttpPost]
public PartialViewResult CreateForm(MyOptionType formOption) {
    MyViewModel model = new MyViewModel();
    model.ApplyOptionValues(formOption);
    return PartialView("_CreateForm", model);
}

注冊路線

// Default
routes.MapRoute(
    "Default", // Route name
    "{controller}/{action}/{id}", // URL with parameters
     new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);

我的問題是,當我單擊單選按鈕時,將執行AJAX請求,但出現“ 404 Not Found”錯誤(即使jQuery函數中的alert似乎顯示了適當的URL)。 昨天我整天都在此上度過,我無法弄清楚到底是什么問題。 我在IIS Express上運行ASP.NET MVC 3應用程序,但我沒有使用Areas(無論如何我都知道)。 有人對如何解決此問題有任何建議嗎? 謝謝。

編輯

警報框顯示以下消息:

表單操作: https://localhost:44300/MyController/CreateForm

形成方式:過帳

編輯

這是重新生成錯誤的整個測試視圖和測試控制器:

視圖

<h2>TestAction</h2>

<script type="text/javascript">
    $(document).ready(function () {
        $("#optionForm input[name='radioOption']").change(function () {
            requestValues($(this).closest("form"));
        });

        var requestValues = function (form) {
            var option = form.find("input:radio:checked").attr("value");

            alert("Form Action: " + form[0].action + "\nForm Method: " + form[0].method);

            if (form.valid()) {
                $.ajax({
                    url: form[0].action,
                    type: form[0].method,
                    data: option,
                    success: function (result) {
                        alert("AJAX success.");
                        //$('#createForm').replaceWith(result);
                    },
                    error: function (xhr) {
                        alert("An error occurred: " + xhr.status + " " + xhr.statusText);
                    }
                });
            }
            return false;
        }
    });
</script>

@using (Html.BeginForm("CreateForm", "Test", FormMethod.Post, new { @id = "optionForm" })) {
    @Html.RadioButton("radioOption", "value1", false, new { @id = "radioButton1" })
    <label for="radioButton1">Radio Button 1</label>
    @Html.RadioButton("radioOption", "value2", false, new { @id = "radioButton2" })
    <label for="radioButton2">Radio Button 2</label>
    @Html.RadioButton("radioOption", "value3", false, new { @id = "radioButton3" })
    <label for="radioButton3">Radio Button 3</label>

    <input type="submit" value="Select" style="display:none;" />
}

<div id="createForm"></div>

CONTROLLER

public class TestController : Controller {
    public ActionResult TestAction() {
        return View();
    }

    [HttpPost]
    public ActionResult CreateForm(string option) {
        return View("TestAction");
    }
}
@using (Html.BeginForm("CreateForm", "MyController", FormMethod.Post, new { id = "optionForm" }))

應該:

@using (Html.BeginForm("CreateForm", "My", FormMethod.Post, new { id = "optionForm" }))

請記住,在ASP.NET MVC幫助器中,您不應傳遞Controller后綴。 這是假定的。

因此正確的網址應為:

https://localhost:44300/My/CreateForm

並不是:

https://localhost:44300/MyController/CreateForm

您顯然擁有MyController類:

public class MyController: Controller
{
    public ActionResult CreateForm(MyOptionType formOption)
    {
        ...
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM