只需要另一組眼睛。錯誤是: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不,這是初始視圖加載 –
如果你的'SelectList'沒有數據,也會出現同樣的錯誤信息 – Izzy