What am I missing here?
ViewModel:
public class ViewModel
{
public IDictionary<int, string> Entities { get; set; }
public int EntityId { get; set; }
}
Controller:
public override ActionResult Create(string id)
{
ViewModel = new ViewModel();
IEnumerable<Entity> theEntities = (IEnumerable < Entity >)db.GetEntities();
model.Entities= theEntities.ToDictionary(x => x.Id, x => x.Name);
return View(model);
}
View:
<div class="editor-field">@Html.DropDownListFor(model => model.EntityId,
new SelectList(Model.Entities, "Id", "Name"))</div>
</div>
Error:
DataBinding: ‘System.Collections.Generic.KeyValuePair….does not
contain a property with the name ‘Id’
KeyValuePairhas propertiesKeyandValue. You are better off declaringEntitiesas typeIEnumerable<Entity>, then your view would work as-is:OR, if you really need to use a Dictionary<>, change your view: