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>
share|improve this question
feedback

1 Answer

up vote 1 down vote accepted

Your link Entry.Paradigm lazy loads a single Paradigm, the one referenced by the foreign key. It does not load all the Paradigm's in the database.

If you want to have a dropdown list of all the paradigms, bound to the selected one. Then you will need a separate ViewBag or Model property that contains a list of the them all.

share|improve this answer
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.