BootstrapDialog模态框
5最近是比较烦直接使用Bootstrap里面的模态框,满屏都是模态框代码,看得心烦。然后想起以前使用的BootstrapDialog.show()的方式,挺简单好用的。然后就拿出来分享一下。
1.下载bootstrap-dialog插件
可以在github下载,下载地址:https://github.com/nakupanda/bootstrap3-dialog
也可以在vs的NuGet搜索bootstrap-dialog下载
2.新建一个mvc项目,命名为BootstrapDialog,通过NuGet搜索bootstrap-dialog下载bootstrap3-dialog,将添加如下文件
3.在App_Start文件下的BundleConfig中添加绑定,如下
4.在Hone控制器中添加DialogDemo方法,并添加DialogDemo试图用来展示
5.DialogDemo界面代码如下:
@{ ViewBag.Title = "DialogDemo"; } <h2>DialogDemo</h2> <button class="btn btn-success" id="alert">BootstrapDialog.alert()</button> <button class="btn btn-primary" id="show">BootstrapDialog.show()</button> <button class="btn btn-danger" id="confirm">BootstrapDialog.confirm()</button> <button class="btn btn-primary" id="load">BootstrapDialog 加载远程页面</button> @section Scripts { <script type="text/javascript"> $('#show').click(function () { BootstrapDialog.show({ title: '提示', message: '请输入验证码', closeable: true, buttons: [{ label: 'Message 1', action: function (dialog) { dialog.setMessage('Message 1'); } }, { label: '确定', action: function (dialog) { dialog.close(); } }] }); }); $('#alert').click(function () { BootstrapDialog.alert({ type: BootstrapDialog.TYPE_WARNING, title: '提示', message: "系统错误!", closeable: true, buttonLabel: "确定" }); }); $('#confirm').click(function () { BootstrapDialog.confirm( { title: '删除提示', message: '是否确定删除?', type: BootstrapDialog.TYPE_WARNING, closable: true, draggable: true, btnCancelLabel: '取消', btnOKLabel: '删除', // <-- Default value is 'OK', btnOKClass: 'btn-warning', callback: function (result) { if (result) { $.ajax({ type: "POST", url: "/Admin/SMS/Delete", data: { id: id }, dataType: "json", success: function (data) { if (data.result == true) { // } else { BootstrapDialog.alert({ type: BootstrapDialog.TYPE_WARNING, title: '提示', message: data.message, buttonLabel: "确定" }); } } }); } } }); }); $("#load").click(function () { BootstrapDialog.show({ title: '加载远程页面', message: function (dialog) { var $message = $('<div></div>'); var pageToLoad = dialog.getData('pageToLoad'); $message.load(pageToLoad); return $message; }, size: BootstrapDialog.SIZE_WIDE, cssClass: "fade", closeable: true, data: { 'pageToLoad': '/Home/Load?msg=' + '我来自遥远的地方...' } }); }); </script> }
6.Home控制器Load方法
public PartialViewResult Load(string msg) { return PartialView("LoadPartial", msg); } view: @model string <h2>这是远程加载的局部页</h2> <p>@Model</p>
7.封装BootstrapDialog
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | function ShowDailog(title,url,success) { BootstrapDialog.show({ title: title, type: BootstrapDialog.TYPE_DEFAULT, size: BootstrapDialog.SIZE_WIDE, cssClass: "fade" , closeable: true , message: function (dialog) { var $message = $( '<div></div>' ); var pageToLoad = dialog.getData( 'pageToLoad' ); $message.load(pageToLoad); return $message; }, data: { 'pageToLoad' : url, }, buttons: [{ label: '<i class="fa fa-close"></i> 取消' , action: function (dialog) { dialog.close(); } }, { label: '<i class="fa fa-check"></i> 确定' , cssClass: 'btn btn-primary' , action: function (dialog) { success(dialog); } }] }); } |
8.调用封装的ShowDailog
function AddMemberSales(t) { var $this = $(t); var type = @((int)PositionType.Member); var parentId =$this.data('key'); var url = '@Url.Action("AddSalesPerson","PersonStruct")?type=' + type + '&parentId=' + parentId; ShowDailog('添加销售人员', url, function (dailog) { var data = $('#team').serialize(); $.ajax({ type: "POST", url: "@Url.Action("AddSalesPerson", "PersonStruct")", data: data, dataType: "json", success: function (result) { if (result.Succeeded) { toastr.success("添数据成功!") setTimeout(function () { //self.location.reload(true); toastr.clear(); }, 1000); $("#squadMemberTmpl").tmpl(result.ReturnValue).insertBefore($this); } else { toastr.error(result.ErrorMessage) } }, error: function () { toastr.error('未知异常导致请求失败,请重试.') } }); dailog.close(); }); }
分类:
javascript
标签:
Jquery
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义