'How to get dropdownlistFor selected value in controller in form of string in MVC

enter image description here

Instead of "A+" i need that value which is selected by user from dropdown list.



Solution 1:[1]

Imagine that you have ModelView Class like below:

Public Class Test
{
    public int SelectedBloodGroup { get; set; }

    public IList<SelectListItem> AvailableBloodGroups { get; set; }
}

Then You Should Fill AvailableBloodGroups in your model in HttpGet of your Controller.

So you should have this in your View:

 <div class="col-md-8">
    <select asp-for="SelectedBloodGroup" asp-items="Model.AvailableBloodGroups">"AvailableBloodGroups"</select>
 </div>

Then with Http Post post your form to your controller and SelectedBloodGroup is then value that User Select it.

use this also in your string to use value

$"where bloodGroup = {model.SelectedBloodGroup}"

Check this Links For More: mvc6-select-tag-helper

Or asp-net-core-custom-drop-down-list

Or MicroSoft-how-make-a-aspfor-dropdown-list

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1