Brauchen Sie einfach einen anderen Satz von Augen. Fehler:MVC DropDownListFor Problem mit Schlüssel
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 -")
-Controller
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>());
}
Modell
public int BrandId { get; set; }
public SelectList BrandForDropDown { get; set; }
..others omitted
Ich habe auch versucht, List<SelectListItem>
auch in dem Modell und der Ansicht, etc, gleichen Fehler
Ist dieser Fehler nach dem Post zurück, wo Sie die gleiche Ansicht zurückgeben? – Izzy
@Izzy nein dies ist erste Ansicht laden –
Die gleiche Fehlermeldung kann auch auftreten, wenn Ihre 'SelectList' keine Daten hat – Izzy