C# ajax表单按常规表单提交

C# ajax表单按常规表单提交,c#,asp.net-mvc,C#,Asp.net Mvc,我有一些ajax表单 @using (Ajax.BeginForm("Action1", "Controller", new AjaxOptions { HttpMethod = "POST",

我有一些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>
    }
所以对于Act2,我想对Act1使用AjaxForm behavior byt,我想做一个简单的页面刷新,并重定向到控制器中的其他页面


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

我认为你需要使用
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
            });
      });
    });
}
剃须刀:

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

我在表单中找到了“dataajax”属性,所以我保持了一切原样,只是在js中更改了“dataajax”属性

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

摆脱Ajax.BeginForm()并使用jquery
.Ajax()
方法,这通常会给您带来更大的灵活性,但在我的情况下,最好将Ajax.BeginForm()与devexpress控件一起使用i delete$(“#我的表单”).submit(函数(事件){}并在CT2上将函数作为按钮的onClick事件。因此,我有请求,但在controller中,我得到了值为空的对象,而不是表单中的值。请检查:谢谢!出于某种原因,
$('myform')。data('ajax','false')
不起作用,但
$('myform')。attr('data-ajax','false')
起作用。