'How can I consume an API in ASP.NET with C#, RestSharp and display the obtained data in a DropdownList?
I need help on how to fill a dropdownlist in ASP.NET with c#, with the data I get from a web API (JSON), I've tried several ways and I can't make it work.
this is my method
protected async Task LlenarDropDowndplGroupArticulosAsync()
{
var client = new RestClient("url");
var request = new RestRequest();
request.AddHeader("Authorization", "Basic token");
request.AddHeader("Content-Type", "text/plain");
request.AddHeader("Cookie", "");
var body = @"{
" + "\n" +
@"
" + "\n" +
@"}";
request.AddParameter("text/plain", body, ParameterType.RequestBody);
RestResponse response = await client.ExecuteAsync(request);
var Lista_desplegable = System.Text.Json.JsonSerializer.Deserialize<Lista_desplegable>(response.Content);
ddlGroupArticulos.DataSource = Lista_desplegable;
ddlGroupArticulos.DataTextField = "Grupo_articulo";
ddlGroupArticulos.DataValueField = "Denominacion";
ddlGroupArticulos.DataBind();
ddlGroupArticulos.Items.Insert(0, new ListItem("Seleccione..", ""));
}
and this is my class
public class Lista_desplegable
{
public string Grupo_articulo { get; set; }
public string Denominacion { get; set; }
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
