How to get DropDownList selected Text

Dondon510 221 Reputation points
2022-10-10T16:24:05.787+00:00

How to get selected text from a dropdownlist?

@azzedinehtmlsql .DropDownListFor(x => x.organizationsPid, Model.organizationNames, new { @id="organizationNames", , new { @id="organizationNames", @class = "form-select" })

example dropdownlist value is:

OrganizationName OrganizationsPid
test 1
testagain 2

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,244 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 58,041 Reputation points
    2022-10-11T18:33:10.51+00:00

    the browser only posts the selected value. so if you are using model binding, on post back model.organizationsPid is the selected value. to get the text, you implement a function that maps the value to the text. if on post back, you rebuild Model.organizationNames (as on post back it will be null), then its:

    var text = model.organizationNames.FirstOrDefault(r =>r == model.organizationsPid);

    0 comments No comments