8
I need to know how to popular a Dropdownlist from another Dropdownlist. Example: I have a Dropdownlist called Project that takes information from my DB. When I select for example "Project 1" I need to load in my second Dropdownlist all Sub Projects belonging to "Project 1". All this information is on DB. I’m not getting popular the second Dropdown. I saw that can be done via Javascript/Json, but I have no idea how to do it. I need help.
Here I populate my first dropdown
public ActionResult CadastrarAtividades()
{
//Lista que recebe todos os PROJETOS ja cadastrados no banco
List<Projeto> projetos = new ProjetoNegocio().Get();
ViewBag.ListaProjeto = new SelectList(projetos.Where(x => x.ProjetoIdPai == null), "ProjetoId", "Nome");
ViewBag.ListaProjetos2 = projetos;
Here I populate my second dropdown. But here I need the data to be according to what was selected in the first dropdown.
//Lista que recebe todos os SUBPROJETOS ja cadastrados no banco
List<Projeto> subprojetos = new ProjetoNegocio().Get();
ViewBag.ListaSubProjeto = new SelectList(subprojetos.Where(x => x.ProjetoIdPai != null), "ProjetoId", "Nome");
View
<div class="panel-body">
<div class="col-lg-10">
@using (Html.BeginForm("CadastrarAtividades", "Apontamento", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{
@Html.AntiForgeryToken()
<br /><br />
<table class="table table-bordered table-hover">
<div class="form-group">
@Html.Label("Projeto")
<div>
@Html.DropDownListFor(model => model.ProjetoId, new SelectList(ViewBag.ListaProjeto, "Value", "Text"), "Selecione", new { @class = "form-control" })
@Html.ValidationMessageFor(m => m.ProjetoId, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.Label("Sub Projeto")
<div>
@Html.DropDownListFor(model => model.ProjetoId, new SelectList(ViewBag.ListaSubProjeto, "Value", "Text"), "Selecione", new { @class = "form-control" })
@Html.ValidationMessageFor(m => m.ProjetoId, "", new { @class = "text-danger" })
</div>
</div>
Did I not understand, because the second dropdown is popular from the first if the information from the second will refer to a different project from the first? Or the information of the second dropdown will be identical to the first?
– Johnny Bigoode
So the information comes from the same DB table. What differentiates one from the other is if the Project has an "Idprojetopai" it is a Sub Project, if it does not have an "Idprojetopai" it is a Project.
– Raphael Gumm
@Raphaelgumm post what ever tried, will help us help you.
– Marconi
As Marconi said, it would be better to explain your situation better: the data coming from DB are with this subproject filter? Did you manage to connect to DB and pull the dice and play in a variable? Or was this data loaded on the first call? The call to DB ta being made as? By MVC? Or have an AJAX call? Try to explain your problem better, if possible maybe a little code, because there are many options to solve your problem.
– Johnny Bigoode
I edited the question. @Johnnybigoode I’m not doing via Javascript.
– Raphael Gumm