I want to load a DropDownList with possible Paises from my database.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Backend;
using Backend.Models;
namespace Frontend_UI_Web.Administrativos
{
public partial class Ciudad : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
PaisRepository paisRepo = new PaisRepository();
LoadPaises(paisRepo);
CiudadRepository ciudadRepo = new CiudadRepository();
}
private void LoadPaises(PaisRepository paisRepo)
{
//FindAll() returns a collection IQueryable<Pais>!
var Paises = paisRepo.FindAll().AsEnumerable();
uiddlPais.DataSource = Paises;
uiddlPais.DataBind();
}
protected void uibtnSubmit_Click(object sender, EventArgs e)
{
}
}
}
Any guidance? If I run this code the dropdownlist loads 5 items, which is correct since I have 5 paises. But the names aren’t displayed correctly.
You’ll want to set the
DataTextFieldandDataValueFieldproperties of the DropDownList so that it knows which properties of eachPaisobject it should bind to.Supposing that
Paislooks likethen you’d want
DataTextField="Name" DataValueField="Id".