How to Fill a DropDownListFor

4

Can anyone help me how do I fill out a DropDownListFor ?

Before I was doing it this way:

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

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

I have my controller where I get the ready list and review for 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 in this DropDownListFor ?

    
asked by anonymous 15.06.2015 / 19:28

2 answers

3

Hello,

Basically for creating 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 "Vehicle" class that has the "idModel" attribute of integer type. At the moment we created a ViewData in the controller with the same attribute name:

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

Automatically .Net verifies that ViewData will be used in the "idVideoClick"

//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" })

Follow the links.

link

link

    
15.06.2015 / 19:57
3

In @Html.DropDownListFor(model => model.CD_AFFILIATE, Model.?, new { @class ="ComboWidth" }) you want to fill 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 by creating a ViewModel class with a property to save or set the Id selected in DropDown, and another property containing the return of Affiliate.filterAffiliate()

See an example I created in .NET Fiddle link that might be useful to you.

    
15.06.2015 / 21:03