I have two classes an Entry and Paradigm. The Entry class has a ParadigmId and a Paradigm property. So in my view I have @Model.Entry.Paradigm
. How do I build a DropDownListFor using the newer syntax for the Paradigm model?
// Entry Model
[Bind(Exclude = "EntryId")]
public class Entry
{
[ScaffoldColumn(false)]
public int EntryId { get; set; }
.
[Display(Name = "Type")]
public int ParadigmId { get; set; }
public virtual Paradigm Paradigm { get; set; }
}
// Paradigm Model
public class Paradigm
{
[ScaffoldColumn(false)]
public int ParadigmId { get; set; }
[Required]
public string Name { get; set; }
public List<Entry> Entries { get; set; }
}
In my view I have @Html.DropDownListFor(model => model.Entry.ParadigmId, model.Entry.Paradigm)
. But the model is of type Paradigm not IEnumerable. Since Paradigm is part of my class (for Entity Framework Code First) I do not need to use a separate ViewData/ViewBag that is listed in most examples.
I Googled a bit and saw individuals using Helper/Extension methods to convert a model into a SelectList. What is the best way to use DropDownListFor in my model?
@* Create View *@
<div class="editor-label">
@Html.LabelFor(model => model.Entry.ParadigmId)
</div>
<div class="editor-field">
@Html.DropDownListFor(model => model.Entry.ParadigmId, model.Entry.Paradigm)
@Html.ValidationMessageFor(model => model.Entry.ParadigmId)
</div>