Question:
I'm developing a registration system using MVC5
with DAO
, but there were some doubts that I couldn't solve.
I have a table where I register Projects and another one where the types of Projects are:
Table: Projects
Fields:
- ProjectProjectId
- ProjName
- ProjectTypeId
Table: ProjectType
Fields:
- ProjectTypeId
- ProjectTypeName
I need to show in my view
a DropDownList
with all projects for the user to select (on registration). And I also need to bring these Types of Projects in the Edit Project page, so that the value registered previously is selected.
After some research I managed to get it to work, but not the way I would like the code to look like this:
(Project Controller)
//Controller GET: Projeto/Edit/5
public ActionResult Editar(int id)
{
//Pesquisar projeto selecionado
ProjetoDAO proj_dao = new ProjetoDAO();
Projeto proj = proj_dao.listById(id);
//Pesquisar todos os TIPOS de projetos
ProjetoTipoDAO proj_tipo_dao = new ProjetoTipoDAO();
List<ProjetoTipo> lista_projetotipo = proj_tipo_dao.ListAll();
//Lista de SelectListItem
List<SelectListItem> lista_ddl = new List<SelectListItem>();
//Percorrer lista de Tipos de Projetos e adicionar na lista de SelectListItem
bool selected = false;
foreach (var item in lista_projetotipo)
{
//Checar se é o ID selecionado
selected = (proj.ProjTipoId.ToString().Equals(item.ProjTipoId.ToString()));
//Adicionar item na lista
lista_ddl.Add(
new SelectListItem()
{
Text = item.ProjTipoNome,
Value = item.ProjTipoId,
Selected = selected
});
}
//Enviando a lista de Tipos de Projetos para a view através de ViewBag
ViewBag.ListaTiposProjeto = lista_ddl;
}
(Edit.cshtml)
<div class="row">
<div class="form-group">
<div class="col-md-2">
@Html.LabelFor(model => model.ProjTipo,
new { @class = "control-label" })
</div>
<div class="col-md-10">
@Html.DropDownList("ListaTiposProjeto", null,
"Selecione um item", new { @class = "form-control" })
</div>
</div>
</div>
This way, I can display the data in DropDownList
and also bring it selected on the edit page, but I can't use ModelState
to validate the form.
- How would I do the same thing using
@Html.DropDownListFor<>
? - Is this really the way?, because I followed some tutorials and they didn't work!
Answer:
You can create a ViewBag with the ProjectType information, it would look something like this:
public ActionResult Editar(int id)
{
//Pesquisar projeto selecionado
ProjetoDAO proj_dao = new ProjetoDAO();
Projeto proj = proj_dao.listById(id);
//Pesquisar todos os TIPOS de projetos
ProjetoTipoDAO proj_tipo_dao = new ProjetoTipoDAO();
List<ProjetoTipo> lista_projetotipo = proj_tipo_dao.ListAll();
SelectList dropDown = new SelectList(lista_projetotipo, "ProjTipoId", "ProjTipoNome");
ViewBag.ListaTiposProjeto = dropDown;
return View(proj);
}
View:
<div class="row">
<div class="form-group">
<div class="col-md-2">
@Html.LabelFor(model => model.ProjTipo, new { @class = "control-label" })
</div>
<div class="col-md-10">
@Html.DropDownListFor(model => model.ProjTipo, (SelectList)ViewBag.ListaTiposProjeto, "Selecione um item", new { @class = "form-control" })
</div>
</div>
</div>
it is necessary that in your Project class it has the Project Type ID and that when retrieving the object this ID has value