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'
KeyValuePair
has properties Key
and Value
. You are better off declaring Entities
as type IEnumerable<Entity>
, then your view would work as-is:
public class ViewModel
{
public IEnumerable<Entity> Entities { get; set; }
public int EntityId { get; set; }
}
OR, if you really need to use a Dictionary<>, change your view:
<div class="editor-field">
@Html.DropDownListFor(model => model.EntityId, new SelectList(Model.Entities, "Key", "Value"))
</div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With