简体   繁体   English

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

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

Here's my code (question found below): 这是我的代码(以下发现问题):

VIEW 视图

// 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 CONTROLLER

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

REGISTER ROUTES 注册路线

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

My issue is that when I click a radio button, the AJAX request executes but I get a "404 Not Found" error (even though the alert in the jQuery function seems to show the appropriate url). 我的问题是,当我单击单选按钮时,将执行AJAX请求,但出现“ 404 Not Found”错误(即使jQuery函数中的alert似乎显示了适当的URL)。 I spent all day yesterday on this, and I cannot figure out what the heck is wrong. 昨天我整天都在此上度过,我无法弄清楚到底是什么问题。 I'm running ASP.NET MVC 3 app on IIS Express, and I'm not using Areas (that I know of anyway). 我在IIS Express上运行ASP.NET MVC 3应用程序,但我没有使用Areas(无论如何我都知道)。 Anyone have any suggestions on how to fix this? 有人对如何解决此问题有任何建议吗? Thanks. 谢谢。

EDIT 编辑

The alert box shows the following message: 警报框显示以下消息:

Form Action: https://localhost:44300/MyController/CreateForm 表单操作: https://localhost:44300/MyController/CreateForm

Form Method: post 形成方式:过帐

EDIT 编辑

Here is an entire test view and test controller that recreates the error: 这是重新生成错误的整个测试视图和测试控制器:

VIEW 视图

<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 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" }))

should be: 应该:

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

Remember that in ASP.NET MVC helpers you should not pass the Controller suffix. 请记住,在ASP.NET MVC帮助器中,您不应传递Controller后缀。 It is assumed. 这是假定的。

So the correct url should be: 因此正确的网址应为:

https://localhost:44300/My/CreateForm

and not: 并不是:

https://localhost:44300/MyController/CreateForm

where you obviously have the MyController class: 您显然拥有MyController类:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM