ASP.NET MVC 3 AJAX请求返回404 Not Found错误

时间:2012-08-14 08:09:38

标签: asp.net-mvc asp.net-mvc-3 jquery asp.net-ajax http-status-code-404

这是我的代码(下面的问题):

查看

// 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应用程序,我没有使用区域(我知道无论如何)。任何人有任何关于如何解决此问题的建议?感谢。

修改

警告框显示以下消息:

  

表单操作: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");
    }
}

1 个答案:

答案 0 :(得分:5)

@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)
    {
        ...
    }
}