【发布时间】:2014-11-03 15:19:44
【问题描述】:
我有一个包含 ajax 表单的 Kendo UI 窗口。我面临的问题是,在成功 POST 之后,我关闭了窗口,我重新打开了窗口以重用它,但是下次我提交表单时它会 POST 两次,依此类推。如果我第三次提交,它将发布三次等等。
我已经尝试过使用 destroy() 函数,从 DOM 中完全删除窗口,然后通过 jQuery 重建它,但效果相同。我还尝试在使用标记代码加载窗口之前清空窗口的内容。
我一直在阅读 Telerik 的 documentation,他们建议将 UpdateTargetId 放在表单之外,我也尝试过同样的效果。我知道缺少某些东西,但我无法确定它。
我应该如何继续使用带有表单的 Kendo Window 作为可重复使用的窗口(不发布与我使用窗口相同的编号)?
我在窗口内的 ajax 表单:
//abbreviated
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")"></script>
<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")"></script>
//abbreviated
<div id="paymentFormId">
@using (Ajax.BeginForm("action", "controller", new { id = id }, new AjaxOptions
{
HttpMethod = "POST",
UpdateTargetId = "paymentFormId",
InsertionMode = InsertionMode.Replace,
LoadingElementId = "loader"
}, new { id = "payment-form" }))
{
@Html.Partial("PaymentForm", new PaymentForm())
}
</div>
//abbreviated
我的 PaymentForm 标记:
//abbreviated
//bunch of text fields to submit
//abbreviated
<button type="submit" class="k-button">Submit</button>
<button onclick="closeWindow()" class="k-button">Close</button>
我的窗口声明:
@(Html.Kendo().Window()
.Name("Window")
.Title("My Win")
.Content("Loading info...")
.Modal(true)
.Draggable()
.Visible(false)
.HtmlAttributes(new { style = "padding: 10px 15px; max-width: 400px;" })
.AutoFocus(true)
.Position(p => p.Top(100).Left(400))
.Events(e => e.Close("onWindowClose"))
)
一些脚本:
//This script is the one that triggers the window to open
function openWindow(id) {
var window = $("#Window").data("kendoWindow");
window.refresh({
url: "/controller/action",
data: {
id: id
},
error: function (xhr, textStatus, exceptionThrown) {
window.close();
alert($.parseJSON(xhr.responseText));
}
});
window.open();
}
//This is the registered close event
function onWindowClose(e) {
var id = $("#ID").val();
if (id != null) {
$.ajax("/controller/action", {
type: "POST",
dataType: "json",
data: {
id: id
},
success: function (data) {
//TODO: derp
},
error: function (xhr, textStatus, exceptionThrown) {
alert($.parseJSON(xhr.responseText));
}
});
}
//var myWin = new $("#Window").data("kendoWindow");
//myWin.destroy();
$(this.element).empty();
$(this.element).html("Loading content...");
}
【问题讨论】:
-
我发现了问题。我从窗口中删除了不显眼的脚本包含到母版页标题,因此该脚本仅包含一次。我没有注意到每次我使用窗口时,脚本也包括在内。将其从窗口中排除后一切正常。
标签: jquery asp.net-mvc razor kendo-ui ajax.beginform