how to Fill a Dropdownlistfor

Asked

Viewed 11,042 times

5

Could someone help me how do I fill one DropDownListFor?

Before I was doing it this way:

@Html.DropDownList("lstAffiliate", string.Empty);

But I saw that it was not recommended, then I decided to do with the ListFor.

I got my controller where I get the list ready and forward to the PartialView:

public ActionResult ComboAffiliate()
{
    FilterAffiliate Affiliate = new FilterAffiliate();
    List<SelectListItem> model = Affiliate.filterAffiliate();

    return PartialView(model);
}

for this Partial

@model  IBS_WEB.Models.Filters.FilterAffiliate

@Html.DropDownListFor(model => model.CD_AFFILIATE, Model.?, new { @class ="ComboWidth" })

How do I list on this DropDownListFor?

3 answers

4

Hello,

Basically for the creation of a Dropdownlistfor you will need to declare the parameters below

@Html.Dropdownlistfor(, )

Model:

public class Veiculo
{
    public int ID { get; set; }            
    public string Tipo { get; set; }
    public int idModeloVeiculo { get; set; }
}

public class ModeloVeiculo
{
    public int ID { get; set; }    
    public string Descricao { get; set; }
}

Action MVC :

[Controller]
ViewData["idModeloVeiculo"] = new SelectList(contexto.ModeloVeiculos.toList(),"ID","Descricao");

HTML :

@model Veiculo

@Html.DropDownListFor(m => m.idModeloVeiculo , null)

As we can see the View is strongly typed for the class "Vehicle" that has the attribute "idModel" of the whole type. The moment we create a Viewdata in the controller with the same attribute name :

[Controller]
ViewData["idModeloVeiculo"] = new SelectList(contexto.ModeloVeiculos.toList(),"ID","Descricao");

Automatically . Net checks that the Viewdata "idModel will be used"

//Na verdade "m.idModeloVeiculo" é ViewData "idModeloVeiculo"
@Html.DropDownListFor(m => m.idModeloVeiculo , null)

Then change your code as follows :

public ActionResult ComboAffiliate()
    {
        FilterAffiliate Affiliate = new FilterAffiliate();
        List<SelectListItem> model = Affiliate.filterAffiliate();

ViewData["Combo"] = new SelectList(Affiliate.filterAffiliate().ToList(),"ID","Descricao");
        return PartialView(model);
    }

Partial :

@model  IBS_WEB.Models.Filters.FilterAffiliate

@Html.DropDownListFor(model => model.CD_AFFILIATE, ViewData["Combo"] ,new { @class ="ComboWidth" })

Follows links.

https://stackoverflow.com/questions/3057873/how-to-write-a-simple-html-dropdownlistfor

https://stackoverflow.com/questions/7142961/mvc3-dropdownlistfor-a-simple-example

3

In @Html.DropDownListFor(model => model.CD_AFFILIATE, Model.?, new { @class ="ComboWidth" }) you want to fill the Model.? with new SelectList().

In other words, you need to tell the dropdown helper how the selection list will be mounted using the new Selectlist.

Ex:

 @Html.DropDownList(
    "Teste", 
    new SelectList(
        Model.Select(x => new { Value = x.CD_AFFILIATE, Text = x.SuaPropriedadeDeTexto}),
        "Value",
        "Text"
    )
)

But I have the impression that you will be happier, creating a Viewmodel class with a property to save or set the selected Id in Dropdown, and another property containing the return of Affiliate.filterAffiliate()

Here’s an example I created in . NET Fiddle https://dotnetfiddle.net/GQbcvs that may be useful to you.

1

    public class TipoVeiculo {
        public int Id { get; set; }
        public string Nome { get; set; }
    }

    public class Veiculo {
        public int Id { get; set; }
        public string Nome { get; set; }

        public int TipoVeiculoId { get; set; }
        public virtual TipoVeiculo { get; set; }
    }

    // No Controller 
    [HttpGet]
    public ActionResult Add()
    {
        var tipos = new List<TipoVeiculo>{ new TipoVeiculo{ id = 1, Nome = "Moto", new TipoVeiculo{ id = 2, Nome = "Carro"} };
        ViewBag.Tipos = tipos;
        return View();
    } 

    // Na View
    @Html.DropDownListFor(
    f => f.TipoVeiculoId,/*Qual atributo,*/
    new SelectList(
    ViewBag.Tipos,/*(fonte de dado,*/
    "Id",/*(Quem vai aparecer o campo value,*/
    "Nome"),/*quem vai aparecer de text),*/
    new { @class = "form-control" })/*atributos do select html*/

    // resumindo fica assim

    @Html.DropDownListFor(f => f.TipoVeiculoId, new SelectList(ViewBag.Tipos, "Id","Nome"), new { @class = "form-control" })

Browser other questions tagged

You are not signed in. Login or sign up in order to post.