2

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?

Raskolnikoov
  • 565
  • 11
  • 27
  • i suspect that the model you are passing does not contain the property `CategoryID` as the error message says, can you check that your domain model has it or the model that you are passing contains the said property – Rafay Sep 17 '11 at 20:34
  • i have CategoryID in my model that im using. I fill the dropdownlist using dynamic. If I follow in debugmode how it all works I get CategoryID and Name from the database. I just dont know how to link CategoryID and Name to value and text field in my selectlist. – Raskolnikoov Sep 18 '11 at 00:22
  • maybe this link will help http://stackoverflow.com/questions/4740969/how-to-databind-a-gridview-to-an-expandoobject/5145419#5145419 – Rafay Sep 18 '11 at 01:12
  • how am i going to use that? i dont really understand that code. – Raskolnikoov Sep 18 '11 at 01:23

3 Answers3

3

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.

lloydphillips
  • 2,775
  • 2
  • 33
  • 58
1

I it looks like there is a Massive method called KeyValues for this purpose. Currently it is line 360 in the source code. It returns a dictionary rather than an Expando. I assume you continue to use your Expando elsewhere in your code.

Here is the method signature:

/// This will return a string/object dictionary for dropdowns etc  
public virtual IDictionary<string, object> KeyValues(string orderBy = "") {...}
Dan Sorensen
  • 11,403
  • 19
  • 67
  • 100
0

I pass my list of categories to my view using ViewData["Categoreis"]

I would recommend you to use models and forget about ViewData/ViewBag. For example define the following view model:

public class MyViewModel
{
    public int CategoryID { get; set; }
    public SelectList Categories { get; set; }
}

and in the controller populate the model and pass to the view:

public ActionResult Index()
{
    var categories = _repository.GetCategories();
    var model = new MyViewModel
    {
        // this assumes that categories is an IEnumerable<T>
        // where T is some domain model having CategoryID and Name properties
        Categories = new SelectList(categories, "CategoryID", "Name")
    };
    return View(model);
}

and finally in your strongly typed view:

@model MyViewModel
@Html.DropDownListFor(x => x.CategoryID, Model.Categories, "--Category--")
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • @Raskolnikoov, did you remove all ViewBag from your application? And when I say all I mean all and from everywhere. – Darin Dimitrov Sep 18 '11 at 11:32
  • Yes there are no viewdata/viewbag. – Raskolnikoov Sep 18 '11 at 11:35
  • @Raskolnikoov, well then the code I have showed should work flawlessly. I have tested it and using it in many of my projects without a glitch. Maybe you could post a narrowed down version of your code which allows to reproduce the problem? – Darin Dimitrov Sep 18 '11 at 11:36
  • Have you ever used Massive ORM (which uses dynamic) for a project? That is what I use on this project. – Raskolnikoov Sep 18 '11 at 12:10
  • I don't see what the ORM that you are using has to do with ASP.NET MVC. You should always define and pass view models to your views from the controller actions and never pass any dynamic or whatever objects. – Darin Dimitrov Sep 18 '11 at 12:12