asp.net-mvc – Asp.Net MVC Ajax.BeginForm没有通过Ajax提交

前端之家收集整理的这篇文章主要介绍了asp.net-mvc – Asp.Net MVC Ajax.BeginForm没有通过Ajax提交前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我的表格如下
  1. <div id="contact-form" class="hidden" title="Online Request Form">
  2. @Using (Ajax.BeginForm("Contact","Main",Nothing,New AjaxOptions With {.UpdateTargetId = "status",.HttpMethod = "post"},New With {.id = "contactUs"}))
  3. @<div>
  4. @Html.LabelFor(Function(m) m.Name)<br />
  5. @Html.TextBoxFor(Function(m) m.Name)<br />
  6. @Html.LabelFor(Function(m) m.Phone)<br />
  7. @Html.TextBoxFor(Function(m) m.Phone)<br />
  8. @Html.LabelFor(Function(m) m.Email)<br />
  9. @Html.TextBoxFor(Function(m) m.Email)<br />
  10. @Html.LabelFor(Function(m) m.Question)<br />
  11. @Html.TextAreaFor(Function(m) m.Question)<br />
  12. @Html.LabelFor(function(m) m.Security)<br />
  13. @Html.TextBoxFor(Function(m) m.Security)<br />
  14. <noscript>
  15. <input type="submit" name="submit" value="Ok" />
  16. </noscript>
  17. @Html.ValidationSummary("Oops,please correct the errors.")<span id="status">@TempData("status")</span>
  18. </div>
  19. End Using
  20. </div>

我在jQuery-UI模态窗口中打开它

  1. <script>
  2. $(function () {
  3.  
  4. // Open the modal dialog from the div.contact-us click event
  5. $('#contact-us').click(function () {
  6. $('#contact-form').dialog('open');
  7. return false;
  8. });
  9.  
  10. // Manage the modal dialog behavior.
  11. $('#contact-form').dialog({
  12. modal: true,autoOpen: false,buttons: {
  13. Cancel: function () {
  14. $(this).dialog('close');
  15. },Ok: function () {
  16. $('form#contactUs').trigger('submit');
  17. }
  18. }
  19. });
  20. });
  21.  
  22. </script>

当我点击“确定”按钮,它发布到相应的控制器,但它不是通过AJAX发布

  1. ''# fix the StackOverflow code coloring issue.
  2. <HttpPost()>
  3. Function Contact(ByVal contactForm As Models.ContactForm) As ActionResult
  4. ViewData("Testimonials") = Helpers.GetTestimonials
  5.  
  6. If ModelState.IsValid Then
  7. ''# Submit the email
  8. TempData("status") = "Thank you,we will be in touch"
  9. Else
  10. ''# Return False
  11. TempData("status") = "Oops,please correct the errors."
  12. End If
  13.  
  14.  
  15. If Request.IsAjaxRequest Then
  16. Return Content(TempData("status").ToString)
  17. Else
  18. Return View("Index")
  19. End If
  20. End Function

我究竟做错了什么?提交表单后,我的网址为http://example.com/Main/Contact,告诉我IsAjaxRequest = false

编辑

即使我不使用jquery-ui“ok”按钮,只需添加< input type =“submit”name =“submit”value =“Ok”/>以表格形式,没有Ajax的表单

解决方法

你有jquery ajax脚本吗?我没有注意到这种行为:
  1. <script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script>

猜你在找的asp.Net相关文章