我有一些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之外还有什么方法可以改变它的行为吗?
答案 0 :(得分:1)
我认为您需要使用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
});
});
});
}
答案 1 :(得分:1)
我找到了数据-ajax&#39;在我的形式属性所以我留下了所有的想法,只是改变了js中的数据-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();
}