我有一些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还会更改其行为之外,还有什么办法吗?
最佳答案
我认为您需要使用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
});
});
});
}