c# - 如何将数据传递给 ASP.NET MVC 中的模式视图
问题描述
我在将数据传递到模态视图时遇到问题。模态视图根本不打开,或者每次打开它时都会从第一个项目中读取数据。我最大的问题是我不知道 jQuery。我想要一个按钮来打开一个模式,在这种情况下读取我的项目的变量 item.Code。项目在 foreach 循环中。
这就是我希望我的代码看起来的样子。
<div class="row" style="padding: 0px 50px">
@foreach (var item in Model)
{
<div style="width: 200px; height:200px; border: 1px dotted #fff; text-align:center; display: inline-block">
<h4>@item.Name</h4>
<!-- Button to open modal view here for this specific item -->
</div>
}
我在有和没有局部视图的情况下都试过了。这是我目前拥有的代码,它只打开一个模式窗口,但不显示其他任何内容。我知道问题出在 id 中,但我试图将所有 id 更改为唯一的,这会导致更多问题。
@foreach (var item in Model)
{
<div style="width: 200px; height:200px; border: 1px dotted #fff; text-align:center; display: inline-block">
<h4>@item.Name</h4>
</div>
<input class="btn btn-default" type="button" value="Details" id="@item.id" onclick="Details(this.id);" />
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
@Html.Raw(item.Code);
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
}
<script>
function Details(id) {
$.get("@Url.Action("preview","Generator")/"+id, function (data) { $('.modal-body').html(data); });
$('#myModal').modal('show');
}
$('#myModal').on('hidden.bs.modal' , function (e) {
$('.modal-body').html("");
})
</script>
谢谢!
解决方案
例如。这是你完成forLoop后的html
<div class="row" style="padding: 0px 50px">
<div style="width: 200px; height:200px; border: 1px dotted #fff; text-align:center; display: inline-block">
<h4>Item Name - 1</h4>
<button type="button" data-item="Item Name - 1" class="btn btn-default clsDetails">Details</button>
</div>
<div style="width: 200px; height:200px; border: 1px dotted #fff; text-align:center; display: inline-block">
<h4>Item Name - 2</h4>
<button type="button" data-item="Item Name - 2" class="btn btn-default clsDetails">Details</button>
</div>
<div style="width: 200px; height:200px; border: 1px dotted #fff; text-align:center; display: inline-block">
<h4>Item Name - 3</h4>
<button type="button" data-item="Item Name - 3" class="btn btn-default clsDetails">Details</button>
</div>
</div>
HTML 模态
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<div class="dvData"></div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
脚本
$(document).ready(function(){
$(document).on("click",".clsDetails",OpenModalPopUp);
});
function OpenModalPopUp(){
var itemName = $(this).data("item");
alert(itemName);
$('#dvData').html(itemName);
$("#myModal").modal();
}
推荐阅读
- bash - Graylog , 在存储之前处理文件
- javascript - 如何将功能组件代码更改为类组件
- python - 过滤字典“喜欢”字符串
- csv - 在 hive 的一列中加载具有“(双引号)的管道分隔 CSV 数据
- javascript - Nextjs 从父目录导入外部组件
- php - 未定义的索引:第 1001 行的 client_id"
- java - 使用 PDFBox 展平 pdf 中的黑框
- mysql - 从 2 个 mySQL 表中获取数据
- javascript - Express.js UnhandledPromiseRejectionWarning:未处理的承诺拒绝
- jmeter - 如何在 HTTP 授权管理器中加密密码参数 - JMETER