我在 modalDialog 中有一个 AjaxForm 正在积累帖子。第一次发好,第二次发两遍,然后发三遍,以此类推……
我发现有几个人有同样的问题,但他们的解决方案都不适合我。click 事件和 GET 总是被调用一次,但是 POST 和 success 函数被调用多次。
这是我的局部视图:
@using (Ajax.BeginForm(new AjaxOptions { OnSuccess = "handleModalSubmit"})) {
@Html.ValidationSummary(true)
@*FORM FIELDS*@
<p>
<input type="submit" class="medium buttons" value="Save" />
</p>
}
在主视图中,它(应该)是这样工作的:
<div id="modal"></div>
<script>
var linkObj;
$('#modal').dialog({
autoOpen: false,
title: 'Ordem',
modal: true,
width: 800,
resizable: false
});
$('.showModal').click(function (e) {
linkObj = $(this);
$.ajax({
url: this.href,
type: 'GET',
cache: false,
success: function (result) {
$('#modal').html('');
$('#modal').html(result).dialog('open');
}
});
//e.stopPropagation();
return false;
});
function handleModalSubmit(result) {
if (result.value) {
$(result.tabelaID).dataTable().fnAddData(result.linha);
$('#modal').dialog('close');
//$('#modal').dialog('destroy');
} else {
// validation failed => refresh the modal to display the errors
$('#modal').html('');
$('#modal').html(result);
}
}
</script>
和控制器:
public ActionResult AddLine()
{
//Add some stuff to ViewBag
return PartialView();
}
[HttpPost]
public ActionResult AddLine(Fatura fatura)
{
if (ModelState.IsValid)
{
db.Faturas.Add(fatura);
db.SaveChanges();
return Json(new
{
value = true,
tabelaID = "idx",
linha = new object[] {"1", 2, 3, 4, 5, 6}
});
}
//Add some stuff to ViewBag
return PartialView(fatura);
}
为什么我会收到重复的帖子?