0

DB に次のようなテーブルがあります。

ここに画像の説明を入力

拡張機能を使用してselectタグにMainTabアイテムを入力したい。Html.DropDownListFor()

string一番難しいのは、これらのアイテムを好きなものにしたいのですが、TabA_Name/TabB_Name/TabC_Nameどうすればいいですか?

4

1 に答える 1

2

ドロップダウン リストがあるページには 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.

于 2012-07-18T18:44:05.737 に答える