我有一个带有按钮的页面,用于显示带有表单的模态部分视图。提交表单时,一切都在服务器端进行,但OnSuccess事件不会被触发。
我的观点
<button id="btnAddLine" class="btn btn-default" data-toggle="modal" data-target="#newOperation">Add Operation</button>
<div id="newOperation" class="modal fade" role="dialog">
<div class="modal-dialog modal-lg" role="document">
<div class="modal-content" id="myModalContent"></div>
</div>
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
<script>
var url = '@Url.Action("DisplayAddOperation")';
$('#btnAddLine').click(function () {
$('#myModalContent').load(url, function () {
$('#newOperation').modal('show');
});
})
var onSubmitSuccess = function (result) {
if (!result.success) {
$('#newOperation').html(result);
} else {
alert('thanks for submitting');
$('#newOperation').dialog('close');
}
};
</script>
}
我的部分视图
<div class="modal-header">
<h4>
Stock
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
</h4>
</div>
@using(Ajax.BeginForm("AddOperation",
"ManagePortfolio",
null,
new AjaxOptions { HttpMethod = "POST", OnSuccess = @"onSubmitSuccess" },
new { @class = "form-horizontal", @role = "form" }))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="modal-body">
......
</div>
<div class="modal-footer">
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
控制器
// GET: ManagePortfolio
public ActionResult Index()
{
List<Stock> stocks = db.Stocks.Include("Description").ToList();
return View(stocks);
}
// GET: AddOperation
public ActionResult DisplayAddOperation()
{
return PartialView("AddOperation");
}
// POST: AddOperation
[HttpPost]
public ActionResult AddOperation()
{
if(ModelState.IsValid)
{
System.Diagnostics.Trace.WriteLine("Operation Added !!!!!");
return RedirectToAction("Index");
}
return new HttpStatusCodeResult(HttpStatusCode.InternalServerError, "Can't find that");
}
正如经常指出的那样,加载了不显眼的脚本。
我还尝试简单地显示OnSuccess事件的警报,例如OnSuccess = "alert('yeah!')"
,但不是更成功。