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