J86
J86

Reputation: 15297

Bind a simple integer array to a DropDownList in MVC3

My ViewModel is as follows:

public class ClassificationCalculatorIndexViewModel
{
    public bool PlacementYear { get; set; }
    public int[] Credit { get; set; } // List of Integers
    public int selectedCredit { get; set; } // Somewhere to store selected integer
    public List<YearOfStudy> StudyYear { get; set; }
}   

I am struggling to bind the Integer array to a DropDownList helper object in my Razor view, I am doing the following:

@Html.DropDownListFor(Model.selectedCredit, Model.Credit)

But getting errors, I tried Googling, but found nothing of the same nature :(

Upvotes: 0

Views: 4233

Answers (2)

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236268

I think you need drop down for selectedCredit filled with items from Credit property:

@Html.DropDownListFor(m => m.selectedCredit, new SelectList(Model.Credit))

Upvotes: 4

Romias
Romias

Reputation: 14133

You need to create a SelectList object with the content of your int[].

Upvotes: 0

Related Questions