Pass text instead of id on dropdown

Asked

Viewed 62 times

2

I wanted to pass the text of the dropdown to the controller(Post) instead of the id, I don’t 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" })

2 answers

0


It was simplistic but it worked:

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

0

Using another construction 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" }
)
  • I do not understand what the argument within the '(Ienumerable<???>)', is giving error.

  • Appendage @using SeuSistema.Models at the top of View.

  • Anocatechesis is an auxiliary class without any kind of link in my template, and it will fill a string type field in my group template. where I’m using a Viewmodel. @model WebAppCatechesis2.ViewModels.GrupoViewModel. You keep saying I don’t have any references on Ienumerable

  • db.AnoCatequese return which Model?

  • model AnoCatequese.

  • So that’s just it. AnoCatequese is not an auxiliary class.

  • It’s an auxiliary table I have no connection to it.

Show 2 more comments

Browser other questions tagged

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