次の問題があります。私のビュー モデルでは、いくつかのリスト プロパティを次のように定義しました。
public class BasketAndOrderSearchCriteriaViewModel
{
List<KeyValuePair> currencies;
public ICollection<KeyValuePair> Currencies
{
get
{
if (this.currencies == null)
this.currencies = new List<KeyValuePair>();
return this.currencies;
}
}
List<KeyValuePair> deliverMethods;
public ICollection<KeyValuePair> DeliveryMethods
{
get
{
if (this.deliverMethods == null)
this.deliverMethods = new List<KeyValuePair>();
return this.deliverMethods;
}
}
}
このビュー モデルは別のビュー モデルに埋め込まれています。
public class BasketAndOrderSearchViewModel
{
public BasketAndOrderSearchCriteriaViewModel Criteria
{
[System.Diagnostics.DebuggerStepThrough]
get { return this.criteria; }
}
}
2 つのアクション メソッドを使用します。1 つは GET 用で、もう 1 つは POST 用です。
[HttpGet]
public ActionResult Search(BasketAndOrderSearchViewModel model){...}
[HttpPost]
public ActionResult SubmitSearch(BasketAndOrderSearchViewModel model){...}
ビューでは、List プロパティの DropDownLists を自動的に表示したくない EditorFor-Html ヘルパーを使用して、ビュー モデル全体を実装します。 1. 質問: EditorFor に DropDownList を表示させるにはどうすればよいですか?
EditorFor を使用して DropDownLists を表示する方法がわからなかったので、DropDownList Html ヘルパーを使用して、次のようにビュー モデルに入力しました。
public IEnumerable<SelectListItem> DeliveryMethodAsSelectListItem()
{
List<SelectListItem> list = new List<SelectListItem>();
list.Add(new SelectListItem()
{
Selected = true,
Text = "<Choose Delivery method>",
Value = "0"
});
foreach (var item in this.DeliveryMethods)
{
list.Add(new SelectListItem()
{
Selected = false,
Text = item.Value,
Value = item.Key
});
}
return list;
}
私の 2. 質問: ご覧のとおり、POST 属性を使用してビュー モデルをアクション メソッドに渡します。渡されたビュー モデルにバインドされた DropDownList の選択された値を取得する方法はありますか? 現時点では、すべての DropDownList が空であり、選択した値は Request.Form によってのみ取得できますが、これは絶対に避けたいです!
これに関するいくつかのアイデアやヒントをいただければ幸いです。