How to get DropDownList selected Text

Dondon510 221 Reputation points

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

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 ( 58,041 Reputation points

    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