Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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);

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top