ドロップダウンリストを作成しようとしていますが、Html.DropDownListレンダリングと戦っています。
私はクラスを持っています:
public class AccountTransactionView
{
public IEnumerable<SelectListItem> Accounts { get; set; }
public int SelectedAccountId { get; set; }
}
これが基本的に今のところ私のビューモデルです。アカウントのリスト、および選択したアイテムを返すためのプロパティ。
私のコントローラーでは、次のようにデータを準備します。
public ActionResult AccountTransaction(AccountTransactionView model)
{
List<AccountDto> accounts = Services.AccountServices.GetAccounts(false);
AccountTransactionView v = new AccountTransactionView
{
Accounts = (from a in accounts
select new SelectListItem
{
Text = a.Description,
Value = a.AccountId.ToString(),
Selected = false
}),
};
return View(model);
}
今問題:
次に、自分のビューでドロップダウンを作成しようとしています。
<%=Html.DropDownList("SelectedAccountId", Model.Accounts) %>
次のエラーが発生します。
キー「SelectedAccountId」を持つViewDataアイテムのタイプは「System.Int32」ですが、タイプは「IEnumerable」である必要があります。
アイテムのリスト全体を返品する必要があるのはなぜですか?選択した値が欲しいだけです。これをどのように行うべきですか?