了解しました。
あなたのケースは ASP.NET Core MVC の 親子ドロップダウン(Cascading Dropdown) の典型的な実装パターンです。
ポイントは、親 <select> の値に応じて子 <select> の内容を Ajax で再取得して再描画することです。
実装手順
1. モデル例
public class PaymentType
{
public int Id { get; set; }
public string Name { get; set; }
}
public class PaymentMethod
{
public int Id { get; set; }
public string Name { get; set; }
public int PaymentTypeId { get; set; }
}
2. コントローラー
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Linq;
public class OrdersController : Controller
{
// 仮のデータ
private static readonly List<PaymentType> paymentTypes = new()
{
new PaymentType { Id = 1, Name = "Credit Card" },
new PaymentType { Id = 2, Name = "Bank Transfer" }
};
private static readonly List<PaymentMethod> paymentMethods = new()
{
new PaymentMethod { Id = 1, Name = "Visa", PaymentTypeId = 1 },
new PaymentMethod { Id = 2, Name = "MasterCard", PaymentTypeId = 1 },
new PaymentMethod { Id = 3, Name = "Mizuho Bank", PaymentTypeId = 2 },
new PaymentMethod { Id = 4, Name = "SMBC", PaymentTypeId = 2 }
};
public IActionResult Edit(int id)
{
ViewBag.PaymentTypeId = new SelectList(paymentTypes, "Id", "Name", 1); // 初期値 1
ViewBag.PaymentMethodId = new SelectList(
paymentMethods.Where(m => m.PaymentTypeId == 1), "Id", "Name"
);
return View();
}
// Ajaxで呼び出すアクション
[HttpGet]
public JsonResult GetPaymentMethods(int paymentTypeId)
{
var methods = paymentMethods
.Where(m => m.PaymentTypeId == paymentTypeId)
.Select(m => new { m.Id, m.Name })
.ToList();
return Json(methods);
}
}
3. View (Edit.cshtml)
@{
Layout = null;
}
<form>
<!-- 親 select -->
<select asp-for="PaymentTypeId" class="form-control"
asp-items="ViewBag.PaymentTypeId" id="parentSelect"></select>
<!-- 子 select -->
<select asp-for="PaymentMethodId" class="form-control"
asp-items="ViewBag.PaymentMethodId" id="childSelect"></select>
</form>
@section Scripts {
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$(document).ready(function () {
// 子 select がクリックされたときに親の値で再構築
$("#childSelect").on("click", function () {
var parentValue = $("#parentSelect").val();
$.getJSON("/Orders/GetPaymentMethods", { paymentTypeId: parentValue })
.done(function (data) {
var $child = $("#childSelect");
$child.empty(); // 既存の項目をクリア
$.each(data, function (i, item) {
$child.append($("<option>", {
value: item.id,
text: item.name
}));
});
})
.fail(function () {
alert("子リストの取得に失敗しました。");
});
});
});
</script>
}
4. 動作概要
- Edit View では親と子の
<select> が初期表示される。 - 子
<select> をクリックすると、親の現在値 (PaymentTypeId) を取得。 - Ajax (
$.getJSON) で GetPaymentMethods を呼び出し、該当する子リストを取得。 - 子
<select> の内容を再構築。
補足
- クリックイベントではなく、親の値が変わったタイミングで更新する場合は
change イベントを使う方が一般的です。 - 初期表示時に親の初期値に基づいて子リストを構築する場合は、
$(document).ready() 内で一度 Ajax 呼び出しを行うと良いです。 asp-for と asp-items の組み合わせは、初期表示用に使い、動的更新は JavaScript で行います。
もし「子 <select> をクリックしたときに親の初期値で再構築」ではなく、
親の値変更時に即更新する方が自然なUIにしたい場合、
次のように $("#parentSelect").on("change", ...) に変えるだけで実現できます。
このコードを使えば、ASP.NET Core MVCで親子関係の <select> を動的に更新できます。
必要なら、初期表示時の Ajax 呼び出しも追加できますが、やりますか?
そうすればページ読み込み直後から最新の子リストが表示されます。
Powered by Froala Editor