Hi,
I helped myself with some resolved discussions on this forum.
But now I'm having trouble finding a tutorial that shows how to implement Cascading DropDownList from a Database.
I'm using ASP.NET MVC but not using EntityFramework with C#.
As of now, I'm able to retrieve the data from my database to my two DropDownList fine.
What I would like to be able to accomplish is to have the user select a UOR first which would then display all Cabina related to that UOR.
The DropDownList Cabina should be disabled and only activated when you select a value from DropDownList UOR.
Any help would be greatly appreciated.
My code below
public class HomeController : Controller
{
[HttpPost]
public ActionResult Index(PersonModel person)
{
var fruitsRepo = new FruitsRepository();
var fruits = fruitsRepo.GetAll();
var fruitsSelecteListItems = fruits.Select(fruit => new SelectListItem
{
Text = fruit.Name,
Value = fruit.Code
}).ToList();
person.Fruits = fruitsSelecteListItems;
var cabinasRepo = new CabinaRepository();
var cabinas = cabinasRepo.GetAll();
var cabinasSelecteListItems = cabinas.Select(cabina => new SelectListItem
{
Text = cabina.NomeCabina,
Value = cabina.CodiceCabina
}).ToList();
person.Nodo = cabinasSelecteListItems;
return View(person);
}
public class Cabina
{
public string NomeCabina { get; }
public string CodiceCabina { get; }
public Cabina(string nomecabina, string codicecabina)
{
NomeCabina = nomecabina;
CodiceCabina = codicecabina;
}
}
public class CabinaRepository
{
public List<Cabina> GetAll()
{
string sql;
var nodo = new List<Cabina>();
string constr = ConfigurationManager.ConnectionStrings["cn"].ConnectionString;
using (MySqlConnection con = new MySqlConnection(constr))
{
sql = @String.Format(" SELECT * FROM `tbl_1` ");
sql += String.Format(" WHERE UOR = '1FU1'; ");
using (MySqlCommand cmd = new MySqlCommand(sql))
{
cmd.Connection = con;
con.Open();
using (MySqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
var cabina = new Cabina(sdr["Nodo_Cod"].ToString(),
sdr["Nodo_Den"].ToString());
nodo.Add(cabina);
}
}
con.Close();
}
}
return nodo;
}
}
public class Fruit
{
public string Code { get; }
public string Name { get; }
public Fruit(string code, string name)
{
Code = code;
Name = name;
}
}
public class FruitsRepository
{
public List<Fruit> GetAll()
{
string sql;
var fruits = new List<Fruit>();
string constr = ConfigurationManager.ConnectionStrings["cn"].ConnectionString;
using (MySqlConnection con = new MySqlConnection(constr))
{
sql = @String.Format("SELECT * FROM `tbl_2`; ");
using (MySqlCommand cmd = new MySqlCommand(sql))
{
cmd.Connection = con;
con.Open();
using (MySqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
var fruit = new Fruit(sdr["Un1"].ToString(),
sdr["Un2"].ToString());
fruits.Add(fruit);
}
}
con.Close();
}
}
return fruits;
}
}
[HttpGet]
public ActionResult Index()
{
var personModel = new PersonModel();
var fruitsRepo = new FruitsRepository();
var fruits = fruitsRepo.GetAll();
var fruitsSelecteListItems = fruits.Select(fruit => new SelectListItem
{
Text = fruit.Name,
Value = fruit.Code
}).ToList();
personModel.Fruits = fruitsSelecteListItems;
var cabinasRepo = new CabinaRepository();
var cabinas = cabinasRepo.GetAll();
var cabinasSelecteListItems = cabinas.Select(cabina => new SelectListItem
{
Text = cabina.NomeCabina,
Value = cabina.CodiceCabina
}).ToList();
personModel.Nodo = cabinasSelecteListItems;
return View(personModel);
}
public ActionResult About()
{
ViewBag.Message = "Your application description page.";
return View();
}
public ActionResult Contact()
{
ViewBag.Message = "Your contact page.";
return View();
}
}
public class PersonModel
{
[Required]
[Display(Name = "UOR")]
public List<SelectListItem> Fruits { get; set; }
[Required]
[Display(Name = "Cabina")]
public List<SelectListItem> Nodo { get; set; }
[Required]
[Display(Name = "Cabina")]
public string SelectedCabina { get; set; }
[Required]
[Display(Name = "UOR")]
public string SelectedFruitCode { get; set; }
}