@Html.DropDownListFor(model=>model.Categories, Model.Categories, Model.CategoryId)
在这里,您没有正确使用助手方法。第一个参数必须是视图模型上的属性,该属性将包含当前选定的值。它应该是标量属性,而不是集合。
因此,在您的视图模型中,您需要添加以下属性:
[Display(Name = "Categorie")]public IEnumerable<SelectListItem> Categories { get; set; }public string SelectedValue { get; set; }在您的控制器动作中:
var selectList = listOfCategories.Select(x => new SelectListItem { Text = x.Name, Value = x.Id.ToString() }).ToList();var viewModel = new BlogModel{ BlogId = blogToEdit.Id, Active = blogToEdit.Actief, Content = blogToEdit.Text, Title = blogToEdit.Titel, Categories = selectList, // this is what sets the selected value SelectedValue = blogToEdit.Category.Id };在您看来,简单来说:
@Html.DropDownListFor(x => x.SelectedValue, Model.Categories)



