0

Could you help me create a dropdown list in asp.net mvc 4?

I Have a ProjetoFuncionario class that is an associative class of "Projeto" and "Funcionario"

I should create a class for the dropdown?

I am having very difficult in creating a dropdown list. Has some good tutorial or a tip you guys can give me?

Thanks!

CLASS PROJETOFUNCIONARIO

namespace Exercicio1.Models
{
    public class ProjetoFuncionario
    {
        public Projeto IdProjeto { get; set; }
        public Funcionario IdFuncionario { get; set; }



    }


}
chewie
  • 333
  • 2
  • 3
  • 19
  • [using-the-dropdownlist-helper-with-aspnet-mvc](http://www.asp.net/mvc/overview/older-versions/working-with-the-dropdownlist-box-and-jquery/using-the-dropdownlist-helper-with-aspnet-mvc) – Grundy Nov 15 '14 at 21:08

2 Answers2

1

A drop down list is used for selecting a value from a list of possible options. Its not clear from you model, but assuming you want a view with drop downs to select a Projeto and a Funcionario, then create a view model to represent what you want to display and edit (What is a view model). Note this example is based on the model definitions from your previous question.

public class ProjetoFuncionarioVM
{
  [Display(Name="Projeto")]
  public long SelectedProjeto { get; set; }
  [Display(Name="Funcionario")]
  public long SelectedFuncionario { get; set; }
  public SelectList ProjetoList { get; set; }
  public SelectList FuncionarioList { get; set; }
}

Controller

public ActionResult Edit()
{
  ProjetoFuncionarioVM model = new ProjetoFuncionarioVM();
  model.SelectedProjeto = // if you want to preselect one of the options
  ConfigureEditModel(model);
  return View(model);    
}

public ActionResult Edit(ProjetoFuncionarioVM model)
{
  if (ModelState.IsValid)
  {
    ConfigureEditModel(model); // reassign the select lists
    return View(model); // return the view to correct errors
  }
  // access properties of the view model, save and redirect
}

private void ConfigureEditModel(ProjetoFuncionarioVM model)
{
  List<Projeto> projeto = // get the collection of projeto objects from the database
  List<Funcionario> funcionario = // ditto for funcionario objects
  // Assign select lists
  model.ProjetoList = new SelectList(projeto, "id_Projeto", "nome_Projeto");
  model.FuncionarioList = new SelectList(funcionario, "id_funcionario", "nom_funcionario");
}

View

@model ProjetoFuncionarioVM

@using (Html.BeginForm())
{
  @Html.LabelFor(m => m.SelectedProjeto)
  @Html.DropDownListForFor(m => m.SelectedProjeto, Model.ProjetoList, "Please select")
  @Html.ValidationMessageFor(m => m.SelectedProjeto)
  @Html.LabelFor(m => m.SelectedFuncionario)
  @Html.DropDownListForFor(m => m.SelectedFuncionario, Model.FuncionarioList, "Please select")
  @Html.ValidationMessageFor(m => m.SelectedFuncionario)
  <input type="submit" value="Save" />
}

Refer also SelectList and DropDownListFor for various overloads of these methods

Community
  • 1
  • 1
  • Where lib i find Configure Edit Model? – chewie Nov 17 '14 at 02:31
  • What do you mean? Its just private method to add in your controller. Its just means you only have to write the code for creating your select lists once (they need to be created in the GET method, and again in the POST method if you return the view) –  Nov 17 '14 at 02:39
  • You shine... Tks so much! you can really help me – chewie Nov 17 '14 at 03:40
0

Check out this link. I would suggest adding a property to your view model for the dropdown list and just make sure you populate it before passing it to your view.

Ju66ernaut
  • 2,592
  • 3
  • 23
  • 36