javascript - 如何打开引导模式窗口弹出窗口而不是在新窗口中打开它?
问题描述
我试图在用户提交表单时弹出一个模式窗口,向用户列出任何验证错误。使用我当前的代码,该窗口将作为一个全新的视图而不是模式窗口打开。我怎样才能让这个窗口与表单的视图重叠而不是打开一个全新的视图?
控制器
[HttpPost]
public IActionResult Daily(Daily dailyReport)
{
var dr = new ReportDaily();
var rc = new ReportDailyCriteria();
dr.Preview(rc, IntPtr.Zero, out Notification notification);
if (notification.HasErrors) {
var error = new Errors();
string errorString = notification.GetConcatenatedErrorMessage(Environment.NewLine + Environment.NewLine);
error.ErrorList = errorString;
return PartialView("_ErrorsModal", error);
}
return View(dailyReport);
}
局部视图
@model Test.Areas.Reports.Models.Errors
<!-- Modal -->
<div id="errorsModal" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title float-left">Error List</h4>
<button type="button" class="close" data-dismiss="modal"></button>
</div>
<div class="modal-body">
<label>Errors: @Model.ErrorList</label>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary" data-dismiss="modal">OK</button>
</div>
</div>
</div>
</div>
解决方案
- 据我了解,您将表单作为完整回发发布到控制器操作。相反,您需要将其作为
ajax
帖子发送,这将使您能够灵活地处理响应。 - 我建议在初始页面加载时呈现您的模式,然后仅
JSON
在从控制器接收结果时使用。它将消除复杂的响应解析逻辑(确定它是部分视图还是其他适当的进一步操作)。
所以在主视图上渲染你的部分视图(Errors: @Model.ErrorList
从部分视图中删除并将标签留空,因为你不再需要它了):
@Html.Partial("_ErrorsModal")
您将返回的控制器操作Json
:
[HttpPost]
public IActionResult Daily(Daily dailyReport)
{
var dr = new ReportDaily();
var rc = new ReportDailyCriteria();
dr.Preview(rc, IntPtr.Zero, out Notification notification);
if (notification.HasErrors)
{
return Json(new
{
success = false,
message = notification.GetConcatenatedErrorMessage(Environment.NewLine + Environment.NewLine)
});
}
return Json(new { success = true });
}
ajax
当您发布表格时您的电话 :
$.ajax({
type: 'POST',
data: JSON.stringify($('#your_form_id').serializeArray().reduce(function(m,o){ m[o.name] = o.value; return m;}, {})),
url: 'http://your_website/your_controller/Daily',
contentType: 'application/json; charset=utf-8',
success: function (data) {
if(data.success){
//your actions when validation successful...
} else {
$('#errorsModal .modal-body label').html(data.message);
$('#errorsModal').modal('toggle');
}
}
});
推荐阅读
- vba - 如何从电子邮件中删除短语?
- javascript - 脚本化的 SVG 动画不再在某些浏览器中工作
- css - 更改下划线样式链接
- docker - 来自此 docker-compose 的跨容器通信不起作用
- jenkins - 将来自 jenkins 的自定义构建输出作为评论提交到 GitHub Pull 请求
- react-native - 在 react-navigation-drawer 中实现“注销”菜单项的正确方法?
- java - 如何解决“尝试在空对象引用上调用虚拟方法'java.lang.String java.lang.Object.toString()'”
- c# - 无法通过 sdk 从 cosmos db 中删除特定文档?
- r - 使用R弹性包在弹性中搜索关键字列表
- python - TensorFlow CNN 得到错误 - 没有属性 Droupout