Ajax 表单像往常一样提交



我有一些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行为byt,我想进行简单的页面刷新并重定向到控制器中的其他页面。

那么除了更改表单 URL 之外还有什么方法可以更改它的行为吗?

我在我的表单中找到了"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();
        }

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

例:

剃刀:

 @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
            });
      });
    });
}

最新更新