【问题标题】:ajax form submit as usual formajax表单照常提交表单
【发布时间】:2017-10-12 22:52:14
【问题描述】:

我有一些 ajax 表单

 @using (Ajax.BeginForm("Action1", "Controller",
                                     new AjaxOptions
                                     {
                                         HttpMethod = "POST",

                                         UpdateTargetId = "content2",
                                         InsertionMode = InsertionMode.Replace,

                                     },
                                     new
                                     {
                                         id = "my_form",
                                         @class = "options_form",
                                         style = "height: 100%; width: 100%; "
                                     }))
    {
        <div id="content2">
            @Html.Partial("_FormPartial", Model)
        </div>
    }

我的 _FormPartial 中有两个按钮调用了两个 js 函数

     function onAct1() {

           $('#my_form').attr('action', '@Url.Action("Action1", "Controller", new {area= "Test" })')
           $('#my_form').submit();
       }

        function onAct2(s, e) {



           $('#my_form').attr('action', '@Url.Action("Action2", "Controller", new {area= "Test" })')
 $('#my_form').submit();
        }

所以对于 Act2,我想为 Act1 使用 AjaxForm 行为字节,我想进行简单的页面刷新并重定向到控制器中的其他页面。

那么除了改变表单Url也改变它的行为之外还有什么办法吗?

【问题讨论】:

  • 摆脱 Ajax.BeginForm() 并使用 jquery .ajax() 方法,这将为您提供更大的灵活性
  • 通常是的,但在我的情况下,最好将 Ajax.BeginForm() 与 devexpress 控件一起使用

标签: c# asp.net-mvc


【解决方案1】:

我在我的表单中找到了“data-ajax”属性,所以我让每个想法都保持原样,只是更改 js 中的“data-ajax”属性。但是感谢所有试图提供帮助的人。

function onAct1() {
$('#my_form').attr('data-ajax', 'false')
$('#my_form').attr('action', '@Url.Action("Action1", "Controller", new {area= "Test" })')
           $('#my_form').submit();
       }




 function onAct2(s, e) {

 $('#my_form').attr('data-ajax', 'true') //not nessesary becouse after Act1 the page will be refreshed and data-ajax  will be true by default
 $('#my_form').attr('action', '@Url.Action("Action2", "Controller", new {area= "Test" })')
 $('#my_form').submit();
        }

【讨论】:

  • 谢谢!出于某种原因,$('#my_form').data('ajax', 'false') 不起作用,但 $('#my_form').attr('data-ajax', 'false') 起作用。
【解决方案2】:

我认为您需要使用Html.BeginForm 而不是Ajax.BeginForm,而您需要使用Act2 方法,您需要调用jQuery ajax。因此,根据您的要求,您需要以下示例:

示例:

剃须刀:

 @using (Html.BeginForm("Action1", "Controller", new {area ="Test"},FormMethod.Post,new { id = "my_form", @class = "options_form",style ="height: 100%; width: 100%; "}))
    {
        <div id="content2">
            @Html.Partial("_FormPartial", Model)
        </div>
    }

脚本:

     function onAct1() {
           $('#my_form').submit();
       }

   function onAct2(s, e) {
    $("#my_form").submit(function(event) {
            $.ajax({
                type: 'POST',
                url: '@Url.Action("Action2", "Controller", new {area= "Test" })',
                async: false,//change as per your requirement
                processData: false,//change as per your requirement
                contentType: false,//change as per your requirement
                dataType: 'json',//change as per your requirement
                data: $("#my_form").serialize()//Pass your form data or else
            }).done(function (json) {
                //Success
            }).fail(function (json) {
                //Error
            });
      });
    });
}

【讨论】:

  • 我删除 $("#my_form").submit(function(event) { } 并将函数 onAct2 作为按钮的 onClick 事件。所以我有请求但在控制器中我得到了具有空值的对象表单中的值
猜你喜欢
  • 1970-01-01
  • 2011-06-17
  • 2012-09-03
  • 1970-01-01
  • 2014-02-19
  • 2011-05-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多