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.

1 answer

  • answered 2022-05-07 04:49 Abolfazl Moslemian

    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

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum