5
Was fehlt mir hier?Eine Dropdown-Liste in mvc3 an ein Wörterbuch binden?
Ansichtsmodell:
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);
}
Ausblick:
<div class="editor-field">@Html.DropDownListFor(model => model.EntityId,
new SelectList(Model.Entities, "Id", "Name"))</div>
</div>
Fehler:
DataBinding: 'System.Collections.Generic.KeyValuePair....does not contain a property with the name 'Id'
hallo , thx für die Antwort, aber meine IDs sind nicht Contig. Die Ergebnisse zurück von der Liste, zu der ich eine Auswahlliste erstellen und die ausgewählte ID speichern möchte, sind wie folgt: [2] = "Joes ent", [9] = "Johns ent" ... usw., ... deshalb möchte ich Erfassen Sie die ausgewählte ID, die der zugeordneten ID im dstabase-Modell entspricht. – Mariah
Dann ändern Sie Ihre Ansicht zu '
Ehrfürchtig, thx !!!!!!! – Mariah