Search code examples
asp.net-mvc-3ormdrop-down-menumassive

Populate dropdownlist using Massive ORM?


Im using MVC 3 and Massive ORM.

I wondering how I could populate a dropdownlist using Massive ORM to get my data from the database.

I pass my list of categories to my view using ViewData["Categoreis"]. It passed the data to the view, but I get this errormessage when I try to load the page in my browser:

DataBinding: 'System.Dynamic.ExpandoObject' does not contain a property with the name 'CategoryID'.

This is how my dropdownlist looks like:

@Html.DropDownListFor(model => model.CategoryID, new SelectList(ViewData["Categories"] as IEnumerable<dynamic>, "CategoryID", "Name"), "--Category--")

Does anybody have a solution for my problem?


Solution

  • I'm using Massive at the moment. Here's how I've populated a countries dropdown from a table in the database:

    This is in my controller:

    DetailsModel model = new DetailsModel();
    var _countries = new Countries(); //Massive class
    model.Countries = _countries.All().Select(x => new SelectListItem { Value = x.Id.ToString(), Text = x.Name });
    

    Here is the Countries property that is inside my DetailsModel

    public IEnumerable<SelectListItem> Countries { get; set; }
    

    In my view:

    @Html.LabelFor(m => m.Country)
    @Html.DropDownList("Country", Model.Countries)
    @Html.ValidationMessageFor(m => m.Country)
    

    Works like a charm for me.