Note that there are some explanatory texts on larger screens.

plurals
  1. POASP.NET MVC 3 AJAX request returns 404 Not Found error
    primarykey
    data
    text
    <p>Here's my code (question found below):</p> <p>VIEW</p> <pre><code>// 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" })) { &lt;div id="options"&gt; @foreach (MyOption op in Model.GetOptions()) { &lt;div class="editor-field"&gt; @Html.RadioButton("formOption", op.OptionType, false, new { @id = op.ID, @title = @op.Description }) &lt;label for="@op.ID"&gt;@op.Name&lt;/label&gt; &lt;/div&gt; } &lt;/div&gt; &lt;input type="submit" value="Select" style="display:none;" /&gt; } </code></pre> <p>CONTROLLER</p> <pre><code>[HttpPost] public PartialViewResult CreateForm(MyOptionType formOption) { MyViewModel model = new MyViewModel(); model.ApplyOptionValues(formOption); return PartialView("_CreateForm", model); } </code></pre> <p>REGISTER ROUTES</p> <pre><code>// Default routes.MapRoute( "Default", // Route name "{controller}/{action}/{id}", // URL with parameters new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults ); </code></pre> <p>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 <code>alert</code> in the jQuery function seems to show the appropriate 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). Anyone have any suggestions on how to fix this? Thanks.</p> <p>EDIT</p> <p>The alert box shows the following message:</p> <blockquote> <p>Form Action: <code>https://localhost:44300/MyController/CreateForm</code></p> <p>Form Method: post</p> </blockquote> <p>EDIT</p> <p>Here is an entire test view and test controller that recreates the error:</p> <p>VIEW</p> <pre><code>&lt;h2&gt;TestAction&lt;/h2&gt; &lt;script type="text/javascript"&gt; $(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; } }); &lt;/script&gt; @using (Html.BeginForm("CreateForm", "Test", FormMethod.Post, new { @id = "optionForm" })) { @Html.RadioButton("radioOption", "value1", false, new { @id = "radioButton1" }) &lt;label for="radioButton1"&gt;Radio Button 1&lt;/label&gt; @Html.RadioButton("radioOption", "value2", false, new { @id = "radioButton2" }) &lt;label for="radioButton2"&gt;Radio Button 2&lt;/label&gt; @Html.RadioButton("radioOption", "value3", false, new { @id = "radioButton3" }) &lt;label for="radioButton3"&gt;Radio Button 3&lt;/label&gt; &lt;input type="submit" value="Select" style="display:none;" /&gt; } &lt;div id="createForm"&gt;&lt;/div&gt; </code></pre> <p>CONTROLLER</p> <pre><code>public class TestController : Controller { public ActionResult TestAction() { return View(); } [HttpPost] public ActionResult CreateForm(string option) { return View("TestAction"); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload