別の目が必要です。エラーは次のとおりです。MVC DropDownListFor問題のキー
The ViewData item that has the key 'BrandId' is of type 'System.Int32'
but must be of type IEnumerable<SelectListItem>.
HTML
@Html.DropDownListFor(x => x.BrandId, Model.BrandForDropDown, "- Brand -")
コントローラ
model.BrandForDropDown = Repository.GetBrandsForDropDown();
public SelectList GetBrandsForDropDown()
{
if (Membership.GetUser() != null)
{
return new SelectList((from store in DataContext.Stores
join userstore in DataContext.UserStores on store.StoreId equals userstore.StoreId
join brand in DataContext.Brands on store.BrandID equals brand.BrandID
where userstore.UserId == userId
select new SelectListItem
{
Value = brand.BrandID.ToString(),
Text = brand.BrandName
}).OrderBy(x => x.Text));
}
return new SelectList(new List<Brand>());
}
モデル
public int BrandId { get; set; }
public SelectList BrandForDropDown { get; set; }
..others omitted
私もList<SelectListItem>
をモデルとビューなどで試してみました。同じエラー
同じビューを返すポストバック後にエラーが発生しますか? – Izzy
@Izzy noこれは初期の表示負荷です –
'SelectList'にデータがない場合にも同じエラーメッセージが表示される可能性があります – Izzy