ASP.NET MVCでBootstrapモーダルにデータを渡す
Bootstrapモーダルにデータを渡す方法
そこで今回は、JavaScript、jQuery、ASP.NET MVCを使って、Bootstrapモーダルに動的にデータを渡す方法を解説します。
データの受け渡し方法
モーダルにデータを渡す方法はいくつかありますが、ここでは最も一般的な2つの方法を紹介します。
1.1 data- 属性を使う*
ボタン要素やアンカー要素に data-*
属性を追加することで、モーダルにデータを渡すことができます。
例えば、以下のコードでは、ボタンをクリックするとモーダルが表示され、data-id
属性の値がモーダルのタイトルに表示されます。
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#myModal" data-id="123">
モーダルを開く
</button>
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel">タイトル</h4>
</div>
<div class="modal-body">
...
</div>
</div>
</div>
</div>
上記のコードをJavaScriptで処理するには、以下のようなコードになります。
$(document).ready(function() {
$('#myModal').on('show.bs.modal', function(event) {
var button = $(event.relatedTarget); // モーダルを開いたボタンを取得
var id = button.data('id'); // data-id属性の値を取得
var modal = $(this); // モーダル要素を取得
modal.find('.modal-title').text('ID: ' + id); // モーダルのタイトルに値を設定
});
});
2 JavaScriptで直接渡す
モーダルを表示するJavaScriptコードの中で、直接データを渡すこともできます。
例えば、以下のコードでは、data
オプションを使ってモーダルにオブジェクトを渡しています。
$(document).ready(function() {
$('#myModal').modal({
data: {
id: 123,
name: 'John Doe'
}
});
});
$(document).ready(function() {
$('#myModal').on('show.bs.modal', function(event) {
var modal = $(this); // モーダル要素を取得
var data = modal.data(); // モーダルに渡されたデータを取得
var id = data.id; // オブジェクトからidプロパティを取得
var name = data.name; // オブジェクトからnameプロパティを取得
modal.find('.modal-title').text('ID: ' + id + ', 名前: ' + name); // モーダルのタイトルに値を設定
});
});
ASP.NET MVCでBootstrapモーダルを使用する場合、以下のようなコードになります。
@Html.Partial("_MyModal")
<script>
$(document).ready(function() {
$('#myModal').modal({
data: {
id: @Model.Id,
name: @Model.Name
}
});
});
</script>
上記のコードでは、_MyModal
という名前の部分ビューをモーダルとして読み込み、Model
プロパティの値をモーダルに渡しています。
まとめ
Bootstrapモーダルにデータを渡す方法はいくつかありますが、今回紹介した2つの方法が最も一般的です。
これらの方法を参考に、ご自身のアプリケーションに合った方法でBootstrapモーダルを活用してください。
参考資料
- [【Bootstrap3】モーダルに「data-*」属性でデータを渡す - Qiita](https
HTML
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Bootstrapモーダルにデータを渡すサンプル</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNhI6S6H+7yj3DZSb12jQAPXDz36KHi4B" crossorigin="anonymous">
</head>
<body>
<div class="container">
<h1>Bootstrapモーダルにデータを渡すサンプル</h1>
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#myModal" data-id="123">
モーダルを開く
</button>
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel">タイトル</h4>
</div>
<div class="modal-body">
<p>ID: <span id="modal-id"></span></p>
<p>名前: <span id="modal-name"></span></p>
</div>
</div>
</div>
</div>
</div>
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM1B5QCF1fB2KEpU8/AcSiYfLL9R5Pw7y9ct6w2d8JnLA" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR3DZMrt8CLjem7yoIcuKUqssIu85u8Wro5F11AKhgAEqavDCaxY0Sq0TR17vQh" crossorigin="anonymous"></script>
<script>
$(document).ready(function() {
$('#myModal').on('show.bs.modal', function(event) {
var button = $(event.relatedTarget); // モーダルを開いたボタンを取得
var id = button.data('id'); // data-id属性の値を取得
var modal = $(this); // モーダル要素を取得
modal.find('#modal-id').text(id); // モーダルのID要素に値を設定
modal.find('#modal-name').text('John Doe'); // モーダルの名前要素に値を設定
});
});
</script>
</body>
</html>
JavaScript
$(document).ready(function() {
$('#myModal').on('show.bs.modal', function(event) {
var button = $(event.relatedTarget); // モーダルを開いたボタンを取得
var id = button.data('id'); // data-id属性の値を取得
var modal = $(this); // モーダル要素を取得
modal.find('#modal-id').text(id); // モーダルのID要素に値を設定
modal.find('#modal-name').text('John Doe'); // モーダルの名前要素に値を設定
});
});
Bootstrapモーダルにデータを渡す他の方法
URLパラメータを使う
モーダルのURLにパラメータを追加することで、データを渡すことができます。
例えば、以下のコードでは、data-id
属性の値をURLパラメータとして渡しています。
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#myModal" data-id="123">
モーダルを開く
</button>
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel">タイトル</h4>
</div>
<div class="modal-body">
<p>ID: <span id="modal-id"></span></p>
</div>
</div>
</div>
</div>
$(document).ready(function() {
$('#myModal').on('show.bs.modal', function(event) {
var url = $(event.relatedTarget).attr('href'); // モーダルのURLを取得
var id = url.substring(url.indexOf('?') + 1); // URLパラメータを取得
var modal = $(this); // モーダル要素を取得
modal.find('#modal-id').text(id); // モーダルのID要素に値を設定
});
});
セッションストレージを使って、モーダルにデータを渡すこともできます。
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#myModal" data-id="123">
モーダルを開く
</button>
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel">タイトル</h4>
</div>
<div class="modal-body">
<p>ID: <span id="modal-id"></span></p>
</div>
</div>
</div>
</div>
$(document).ready(function() {
$('#myModal').on('show.bs.modal', function(event) {
var id = $(event.relatedTarget).data('id'); // data-id属性の値を取得
sessionStorage.setItem('modal-id', id); // セッションストレージに値を保存
var modal = $(this); // モーダル要素を取得
modal.find('#modal-id').text(id); // モーダルのID要素に値を設定
});
});
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#myModal" data-modal-id="123">
モーダルを開く
</button>
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×
javascript jquery asp.net-mvc