@Html.DropDownListFor
2 つのヘルパーを含むビューがあります。
<div class="editor-field">
@Html.DropDownListFor(model => model.Employee, (IEnumerable<SelectListItem>)ViewBag.emps, "--Select--", new { style = "width:150px", id = "ddl1"})
@Html.ValidationMessageFor(model => model.Employee)
</div>
<div class="editor-field">
@Html.DropDownListFor(model => model.Employee2, (IEnumerable<SelectListItem>)ViewBag.emps, "--Select--", new { style = "width:150px", id = "ddl2"})
@Html.ValidationMessageFor(model => model.Employee2)
</div>
それらは次のように入力されます。
public ActionResult Create()
{
List<Employees> emps = new List<Employees>();
emps.Add(new Employees { Id = 0, Name = "Michael Jordan" });
emps.Add(new Employees { Id = 1, Name = "Magic Johnson" });
emps.Add(new Employees { Id = 2, Name = "Larry Bird" });
var items = emps.Select(i => new SelectListItem
{
Value= i.Id.ToString(),
Text =i.Name
});
ViewBag.emps = items;
return View();
}
最初の DDL で選択した項目を 2 番目の DDL から削除するにはどうすればよいですか? 次のようにjQueryを使用して、選択したアイテムを取得できました。
<script type="text/javascript">
$(function () {
$("#ddl1").change(function () {
alert($("#ddl1").val());
});
});
</script>
しかし、私はそれを私の目的に使用する方法を見つけることができませんでした。