0

Here is what I have:

var listAddresses = GetAddresses().ToList();
return Json(new JsonResult { Data = new SelectList(listAddresses, "Name", "Id") }, JsonRequestBehavior.AllowGet);

But I get the error 'System.Dynamic.ExpandoObject' does not contain a property with the name 'Name'.

listAddresses is consisted of 10 items. When I debug, when I watch each one of them, I go to Dynamic View and there is Name and Id. How to recolve this?

petko_stankoski
  • 10,459
  • 41
  • 127
  • 231

3 Answers3

0
var listAddresses = GetAddresses().ToList();
var data = new 
{ 
    Data = new SelectList(listAddresses, "Name", "Id") 
};
return Json(data, JsonRequestBehavior.AllowGet);

Json(...) is a JsonResult, you don't need both.

asawyer
  • 17,642
  • 8
  • 59
  • 87
0

try like this-->

var listAddresses = GetAddresses().ToList(); 
        List<ListItem> addressList = new List<ListItem>();
        foreach (IAddress address in listAddresses )
            {
                ListItem items = new ListItem();
                items.Text = address.Name;
                items.Value = address.ID;
                addressList .Add(items);


            }
        }
    return Json(new JsonResult { Data = new SelectList(addressList, "Value", "Text") }, JsonRequestBehavior.AllowGet);
Baskaran
  • 273
  • 1
  • 3
  • 13
0

You cannot use dynamic features of C# without using the dynamic keyword. So:

var listAddreses = GetAddresses().ToList();

leaves you with a List<ExpandoObject> which really truly does not have any of the properties you mentioned. However if you say:

List<dynamic> listAddresses = GetAddresses().ToList();

It should work.

Chris Pfohl
  • 18,220
  • 9
  • 68
  • 111