简体   繁体   English

ASP.NET MVC 2从JavaScript发布AJAX

[英]ASP.NET MVC 2 Post AJAX from from JavaScript

I got control with strongly typed View, with Ajax.BeginForm(). 我使用Ajax.BeginForm()对强类型视图进行了控制。 Now I would like to change submit method from 现在我想从

<input type="submit" id="testClick" value="Submit" />

To some javascript method DoSubmit(). 到一些javascript方法DoSubmit()。 What I tried is : 我试过的是:

  • Invoke click on that submit button 调用单击该提交按钮
  • Invoke submit on form ('form1').submit(), document.forms['form1'].submit() 调用表单('form1')。submit(),document.forms ['form1']。submit()提交
  • jQuery forms with ('form1').AjaxSubmit(); 具有('form1')。AjaxSubmit();的jQuery表单
  • Create jQuery AJAX 创建jQuery AJAX

     $.ajax({ type: "POST", url: $("#form1").attr("action"), data: $("#form1").serialize(), success: function() { alert("epic win!!!1!1!") }, error: function(XMLHttpRequest, textStatus, errorThrown) { alert("epic fail!") } }); 

All those method created normal request (not AJAX), or they didn't work. 所有这些方法都创建了普通请求(不是AJAX),或者它们不起作用。 So anyone know how I can do AJAX submit "Form", from JavaScript and strongly typed mechanism (public AcrionResult MyFormAction(FormModel model); ) will work? 因此,谁知道我该如何通过JavaScript和强类型化机制(公共AcrionResult MyFormAction(FormModel model);)来执行AJAX提交“表单”的工作?

I've had this work fine for me using the forms plugin for jquery. 我使用jquery的表单插件对我来说工作很好。 What I found tho, was that I had to handle the click event, do the ajax submit and then return false to ensure that the normal post didn't occur. 我发现,是我必须处理click事件,执行ajax提交,然后返回false以确保不会发生正常的发布。

<script type="text/javascript">
    $('#testClick').click(function(){
        $('#form1').ajaxSubmit();
        return false;
    });
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM