这是我的代码(问题如下):
看法
// 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);
}
注册路线
// 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”错误(即使alert
jQuery 函数中似乎显示了适当的 url)。我昨天花了一整天的时间在这上面,我不知道到底出了什么问题。我在 IIS Express 上运行 ASP.NET MVC 3 应用程序,但我没有使用区域(无论如何我都知道)。有人对如何解决这个问题有任何建议吗?谢谢。
编辑
警报框显示以下消息:
表单操作:
https://localhost:44300/MyController/CreateForm
表单方式:post
编辑
这是重新创建错误的整个测试视图和测试控制器:
看法
<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");
}
}