0

这里是我的代码(以下发现的问题):ASP.NET MVC 3 AJAX请求返回404 Not Found错误

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;" /> 
} 

控制器

[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 
); 

我的问题是,当我点击一个单选按钮,执行AJAX请求,但我得到了“404 Not Found”错误(尽管jQuery函数中的alert似乎显示了适当的url)。我昨天花了整整一天的时间,我无法弄清楚什么是错误的。我在IIS Express上运行ASP.NET MVC 3应用程序,并且我没有使用区域(我知道的)。任何人有任何建议如何解决这个问题?谢谢。

EDIT

警报框显示以下消息:

Form Action: https://localhost:44300/MyController/CreateForm

Form Method: post

EDIT

这里是,再现误差的整个测试图和测试控制器:

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> 

控制器

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

    [HttpPost] 
    public ActionResult CreateForm(string option) { 
     return View("TestAction"); 
    } 
} 
+0

建议您使用诸如Fiddler工具,或Chrome中的网络工具(Ctrl Shift I) - 404将以红色突出显示,您可以看到有问题的URL秒。 – StuartLC 2012-08-14 08:12:30

+0

MyController是控制器的真实名称吗?你能否给我们更多的细节来尝试再现你的问题? – 2012-08-14 08:13:47

+0

你可以发布警报中显示的内容吗? – nemesv 2012-08-14 08:19:30

回答

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后缀。假定。

所以正确的URL应该是:

https://localhost:44300/My/CreateForm 

,而不是:

https://localhost:44300/MyController/CreateForm 

,你明明有MyController类:

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

'MyController'示例名称是一个糟糕的选择。正如你所提到的,真正的代码省略了'Controller'部分。我发布了新的测试代码,可能会更好。我对这种混乱表示抱歉。 – neizan 2012-08-14 09:35:09

+0

'data:option'应该是'data:{option:option}'。还有你在哪里看到这个404错误?在FireBug中查看AJAX请求时?你似乎也在使用HTTPS。你确定这是在IIS Express中正确配置的吗?请不要忘记IIS Express中用于HTTPS的端口与用于HTTP服务的端口不同。 – 2012-08-14 09:37:38

+0

我会尝试更改数据,但我确实已从ajax代码中删除数据,并从调用的操作方法中删除参数,并生成相同的错误。现在,关于404错误,我从ajax代码的'error:...'部分的alert框中看到它。 Https ...我以为我已经设置好使用它,因为我的其他控制器/操作组合都在https上工作,并且在尝试使用ajax之前我没有遇到任何问题。 – neizan 2012-08-14 09:45:21