I need to know how to populate a DropDownList from another DropDownList. Example: I have a DropDownList named Project that takes the information from my DB. When I select for example "Project 1" I need in my second DropDownList to load all Sub Projects belonging to "Project 1". All this information is in DB. I'm not getting popular the second DropDown. I saw what you can do via JavaScript / Json, but I have no idea how to do it. I need some 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>