【发布时间】:2012-08-10 12:06:31
【问题描述】:
这是我的代码(问题如下):
查看
// 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”错误(即使 jQuery 函数中的 alert 似乎显示了适当的 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");
}
}
【问题讨论】:
-
建议您使用 Fiddler 之类的工具,或 Chrome 中的网络工具(Ctrl Shift I) - 404 将以红色突出显示,您可以看到有问题的 URL 是什么。
-
MyController是控制器的真名吗?你能告诉我们更多细节来尝试重现你的问题吗?
-
您能发布警报中显示的内容吗?
-
您确定使用 https 和端口 44300 吗?我猜你的 iis express 配置错误。
-
@nemesv,我已在编辑中添加了警报消息。
标签: asp.net-mvc asp.net-mvc-3 jquery asp.net-ajax http-status-code-404