我的表格如下
<div id="contact-form" class="hidden" title="Online Request Form">
@Using (Ajax.BeginForm("Contact", "Main",
Nothing,
New AjaxOptions With {.UpdateTargetId = "status", .HttpMethod = "post"},
New With {.id = "contactUs"}))
@<div>
@Html.LabelFor(Function(m) m.Name)
@Html.TextBoxFor(Function(m) m.Name)
@Html.LabelFor(Function(m) m.Phone)
@Html.TextBoxFor(Function(m) m.Phone)
@Html.LabelFor(Function(m) m.Email)
@Html.TextBoxFor(Function(m) m.Email)
@Html.LabelFor(Function(m) m.Question)
@Html.TextAreaFor(Function(m) m.Question)
@Html.LabelFor(function(m) m.Security)
@Html.TextBoxFor(Function(m) m.Security)
<noscript>
<input type="submit" name="submit" value="Ok" />
</noscript>
@Html.ValidationSummary("Oops, please correct the errors.")<span id="status">@TempData("status")</span>
</div>
End Using
</div>
我在jQuery-UI模态窗口中打开它
<script>
$(function () {
// Open the modal dialog from the div.contact-us click event
$('#contact-us').click(function () {
$('#contact-form').dialog('open');
return false;
});
// Manage the modal dialog behavior.
$('#contact-form').dialog({
modal: true,
autoOpen: false,
buttons: {
Cancel: function () {
$(this).dialog('close');
},
Ok: function () {
$('form#contactUs').trigger('submit');
}
}
});
});
</script>
当我单击“确定”按钮时,它会发布到相应的控制器,但不会通过AJAX发布
''# fix the StackOverflow code coloring issue.
<HttpPost()>
Function Contact(ByVal contactForm As Models.ContactForm) As ActionResult
ViewData("Testimonials") = Helpers.GetTestimonials
If ModelState.IsValid Then
''# Submit the email
TempData("status") = "Thank you, we will be in touch"
Else
''# Return False
TempData("status") = "Oops, please correct the errors."
End If
If Request.IsAjaxRequest Then
Return Content(TempData("status").ToString)
Else
Return View("Index")
End If
End Function
我究竟做错了什么?提交表单后,我的网址是http://example.com/Main/Contact,告诉我 IsAjaxRequest = false
编辑
Even when I don't use the jquery-ui "ok" button and simply add <input type="submit" name="submit" value="Ok" /> to the form, the form posts without Ajax
2 回答
你有包含jquery ajax脚本吗?当我没有包含它时,我注意到了这种行为:
我有一段时间没有使用ASP.NET MVC AJAX助手,但我相信
Ajax.BeginForm
在生成的(HTML)标记中添加了一个内联onsubmit
处理程序 .如果是这种情况,在此表单上调用submit programmatically (触发
submit
事件的jQuery)将不会调用表单的onsubmit
函数,这很可能会取消正常的提交操作并通过AJAX提交表单 .有关发生这种情况的更多信息,请查看this related answer .
作为替代方案,您可以使用AJAX发布表单
jQuery form plugin .
jQuery's built-in AJAX function .
我发现这些方法中的任何一种都不如使用微软的AJAX助手那么麻烦 .