0

I have for the most part successfully connected to an API endpoint and manage to deserialize nested json result and bind it to a gridview using Newtonsoft.Json serialization attributes.

I however cannot bind to a dropdownlist. What is the correct property name to use to pass the supplier name to my dropdownlist?

I can see the property I want to pass (supplier name) and have tried all possible strings I can think of but all I get is the class name to display.

The Supplier Name displays fine on the gridview enter image description here

I can see the property I want to display supplier -> name

enter image description here

Binding Code

var readdata = comsumeapi.Result;
            if (readdata.IsSuccessStatusCode)
            {
                var displayrecords = readdata.Content.ReadAsAsync<IList<CoupaPODetails>>();
                displayrecords.Wait();
                empobj = displayrecords.Result;              
                GridView1.DataSource = empobj;
                GridView1.DataBind();
                DropDownList1.DataSource = empobj;
                DropDownList1.DataTextField = "supplier";
                DropDownList1.DataBind();
            }
Hector Marcia
  • 103
  • 1
  • 12

1 Answers1

1

It would have been quite helpful to see your JSON object code but I think I can glean what I need from the screenshots

You've bound the drop down list to supplier object, not the name of the supplier. I think you should probably make a new list of all the different suppliers and bind to that, something like:

var x = empobj.Select(e =>  e.supplier.name).Distinct().ToList();

(Your supplier object only seems to contain a name? This a bit odd why there would even be a supplier object at all if it only houses a string. I figured it might contain more than that , like a name and an ID. If it contains more than that and you want a display text and a value that are different, use one of the techniques from here to group by eg the value and then linq .Select(g => new ListItem(){Text = g.First(), Value = g.Key}) to generate a List<ListItem> that can be the datasource for your drop down)

Don't forget that you'll also need to bind to the grid's row data bound event to set the selected item in the drop down, detail for which is here

Caius Jard
  • 72,509
  • 5
  • 49
  • 80
  • that did it Caius. The json file is heavily nested with more than 10k lines. I'm testing with a small set. I will however try adding another set of key:value to the supplier object. The last link refers to, I think, when the the dropdown menu is within the grid cell. not my case here (yet) – Hector Marcia Mar 14 '20 at 16:05
  • Ah, yes I see it now. Because it's so big and the drop down overhangs the grid I thought it had emerged from it but I see the combo box at the top now. In that case the last paragraph can probably be ignored – Caius Jard Mar 14 '20 at 16:51