【发布时间】:2018-09-24 17:48:38
【问题描述】:
所以,经过大量调试后,我知道在我的应用程序中包含一个带有几个复选框和一个提交按钮的局部视图,控制器/JS 被调用了两次。 这是转折点:
- 当 JS 被调用两次时。在第一个请求中,它保存复选框的正确值,然后在下一个请求中保存以前可用的值。
- 此调用不会始终如一地发生,它是随机发生的。还检查了数据或代码是否存在任何问题,但没有问题。所以问题是在 AJAX 调用本身产生的,但我无法找到原因。
谁能帮我解决这个问题。我真的很绝望。 我的代码如下:
我的部分观点:
using (Ajax.BeginForm("action", "controller", new AjaxOptions { OnComplete = "TimeLineObject.OnAjaxComplete", OnSuccess = "", OnBegin = "TimeLineObject.OnAjaxBegin", UpdateTargetId = "divEditorContent", OnFailure = "insertfailure" }, new { @class = "form-inline", id = "searchForm" }))
{
@Html.AntiForgeryToken()
@Html.HiddenFor(m =>m.IsSave)
<table class="table table-responsive">
<tr>
<th>#</th>
<th>Document </th>
<th>Input Type</th>
</tr>
@for (int i = 0; i < Model.model.Count; i++)
{
<tr>
<td>
@Html.HiddenFor(m => Model.model[i].Id)
@Html.HiddenFor(m => Model.model[i].Id)
@Html.CheckBoxFor(m => Model.model[i].IsChecked, new { @class = "form-control" })
</td>
<td>
@Model.model[i].DocumentName
</td>
<td>
@Model.model[i].InputType
</td>
</tr>
}
</table>
}
我的控制器:
[OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]
[ValidateInput(true), ValidateAntiForgeryToken]
public ActionResult action(object documents)
{
using (CaseConfigManager manager = new CaseConfigManager())
{
var response = manager.layer2(documents.data, SessionId);
if (response.Status)
{
return this.PartialView("_Message", new MessageModel() { Type = MessageType.Success, Message = response.Message, Code = ClientStatussCode.TimeLine });
}
else
{
return this.PartialView("_Message", new MessageModel() { Type = MessageType.Warning, Message = response.Message, Code = ClientStatussCode.TimeLine });
}
}
}
现在保存按钮位于生成部分视图的时间轴页面中。
时间线 JS:
OnAjaxBegin: function () {
debugger;
$('#formsave').attr('disabled', true);
},
OnAjaxComplete: function () {
debugger;
$('#formsave').attr('disabled', false);
RegisterAutoCompute();
RegisterValidation();
TimeLineObject.RegisterDatePicker();
},
OnAjaxSuccess: function (HTML, Message) {
TimeLineObject.FillHtmlContent(HTML);
TimeLineObject.OnCloseClick();
TimeLineObject.Message(Message);
},
OnAjaxFailure: function (error) {
TimeLineObject.Message(TimeLineObject.Alerts.AjaxFail);
},
上面的 OnAjaxBegin 被调用了两次。
【问题讨论】:
标签: javascript jquery ajax asp.net-mvc