DB に次のようなテーブルがあります。
拡張機能を使用してselect
タグにMainTab
アイテムを入力したい。Html.DropDownListFor()
string
一番難しいのは、これらのアイテムを好きなものにしたいのですが、TabA_Name/TabB_Name/TabC_Name
どうすればいいですか?
DB に次のようなテーブルがあります。
拡張機能を使用してselect
タグにMainTab
アイテムを入力したい。Html.DropDownListFor()
string
一番難しいのは、これらのアイテムを好きなものにしたいのですが、TabA_Name/TabB_Name/TabC_Name
どうすればいいですか?
ドロップダウン リストがあるページには View Model を使用します。例えば、
public class MyViewModel
{
/* You will keep all your dropdownlist items here */
public IEnumerable<SelectListItem> Items { get; set; }
/* The selected value of dropdown will be here, when it is posted back */
public String DropDownListResult { get; set; }
}
ビューモデルをビューに返すコントローラーで、リストを埋めてそのモデルを返します。
public ActionResult Create()
{
/* Create viewmodel and fill the list */
var model = new MyViewModel();
// TODO : Select all data from MainTab to variable. Sth like below.
var data= unitOfWork.Reposityory.GetAll();
/* Foreach of the MainTab entity create a SelectListItem */
var dropDownListData = data.Select().(x = > new SelectListItem
{
/* Value of SelectListItem is the pk of MainTab entity. */
Value = x.MainTabID,
/* This is the string you want to display in dropdown */
Text = x.TabA.Name + "/" + x.TabB.Name + "/" + x.TabC.Name
});
model.Items = new SelectList(dropdownListData, "Value", "Text");
return View(model);
}
これはあなたの見解です。
/* Make your view strongly typed via your view model */
@model MyNamespace.MyViewModel
/* Define your dropdown such that the selected value is binded back to
* DropDownListResult propery in your view model */
@Html.DropDownListFor(m => m.DropDownListResult, Model.Items)
ビューをコントローラーに戻すと、ビューモデルにはDropDownListResult that is filled with the selected dropdownlist item.