asp.net-mvc - ASP.net MVC - 如何设置模式?
问题描述
我真的不确定如何在 ASP.NET MVC 中处理模式对话框:情况如下:在我的 home/index 操作方法中,我检查用户是否已经接受了条款和条件。如果没有,我想显示一个对话框以提示用户确认。
现在我真的不确定如何在 MVC 中解决这个问题:我看到的可能性:
在 ViewBag 中存储 TaCConfirmNeeded 属性,在我看来,请注意 viewbag 包含此标记,显示带有表单的 jquery 模态对话框以确认条款和条件,并从此表单调用 account/confirmTA。
在我的共享文件夹中创建一个视图,该视图使用我的常用布局,其样式类似于模式对话框,并带有一个发送到 account/confirmTA 的表单。
有没有更好/更容易的可能性?
解决方案
这就是我在 MVC 中实现模态弹出窗口的方式。希望它可以帮助你。
创建局部视图(示例):
@model CodeFirst.Models.FriendsInfo
<div>
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title" id="myModalLabel">FriendsInfo</h4>
</div>
<hr />
<dl class="dl-horizontal">
<dt>
@Html.DisplayNameFor(model => model.Name)
</dt>
<dd>
@Html.DisplayFor(model => model.Name)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Mobile)
</dt>
<dd>
@Html.DisplayFor(model => model.Mobile)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Address)
</dt>
<dd>
@Html.DisplayFor(model => model.Address)
</dd>
</dl>
控制器看起来像:
public ActionResult Details(int Id)
{
FriendsInfo frnds = new FriendsInfo();
frnds = db.FriendsInfo.Find(Id);
return PartialView("_Details",frnds);
}
稍后把它放在你的 _Layout 上:
<div id='myModal' class='modal'>
<div class="modal-dialog">
<div class="modal-content">
<div id='myModalContent'></div>
</div>
</div>
</div>
从你的角度来看,你需要这个带有一点 jQuery 的脚本来用 AJAX 调用它:
<script>
var PostBackURL = '/Home/Details';
$(function () {
// Use your logic, to call this function and replace the parameters like the User.ID
debugger;
var id = User.Id;
var options = { "backdrop": "static", keyboard: true };
$.ajax({
type: "GET",
url: PostBackURL ,
contentType: "application/json; charset=utf-8",
data: { "Id": id },
datatype: "json",
success: function (data) {
debugger;
$('#myModalContent').html(data);
$('#myModal').modal(options);
$('#myModal').modal('show');
},
error: function () {
alert("Dynamic content load failed.");
}
});
});
//$("#closebtn").on('click',function(){
// $('#myModal').modal('hide');
$("#closbtn").click(function () {
$('#myModal').modal('hide');
});
</script>
推荐阅读
- sql - 如何根据条件选择左连接
- android - Android : 如何知道在 android 中连接到我的热点的 ip
- html - 如何使这两个按钮居中,一个在另一个的右侧?
- aspnetboilerplate - 如何仅审核 [已审核] 方法而没有 [DisabledAudited] 无处不在?
- amazon-web-services - 通过 docker 安装的 Jenkins 无法在 AWS EC2 上运行
- ios - UICollectionView 水平滚动无法正常工作
- c - 关于输入/输出的 C 语言程序无法正常运行
- java - 在 InteliJ 控制台 mac OS X 中运行 java 应用程序
- java - 在 DFS 中将节点标记为已访问以进行词搜索
- angular - Angular Dynamic Mat 选项卡 - 如何将新创建的选项卡添加为第一个选项卡