Pass the text instead of the id in the dropdown

2

I wanted to pass the dropdown text to the controller (Post) instead of the id, I do not know how I can do this.

Controller:

  ViewBag.AnoCatequeseID = new SelectList(db.AnoCatequese, "AnoCatequeseID", "Ano");

View:

 @Html.DropDownListFor(model => model.grupo.AnoCatequese, (SelectList)ViewBag.AnoCatequeseID, "--Escolha um ano de Catequese--", htmlAttributes: new { @class = "form-control" })
    
asked by anonymous 11.03.2016 / 16:42

2 answers

0

It was simplistic but it worked:

ViewBag.AnoCatequeseID = new SelectList(db.AnoCatequese, "Ano", "Ano");
    
14.03.2016 / 18:29
0

Using another construct of @Html.DropDownListFor :

@Html.DropDownListFor(model => model.grupo.AnoCatequeseID, 
    ((IEnumerable<AnoCatequese>)ViewBag.AnoCatequeseID).Select(ac =>
        new SelectListItem {
            Text = ac.Ano,
            Value = ac.AnoCatequeseID,
            Selected = (Model.grupo != null) && (Model.grupo.AnoCatequeseID == ac.AnoCatequeseID)
        }), 
    "--Escolha um ano de Catequese--", 
    htmlAttributes: new { @class = "form-control" }
)
    
11.03.2016 / 17:02