You must Sign In to post a response.
  • Category: ASP.NET

    How to set selected value in dropdown in mvc

    Hi i am bindiing dropdown with hardcoded value . Now i want to set value=M on searching button click using viewbag. How can i set in view .Thanks in advance

    @Html.DropDownListFor(m => m.Gender, new List<SelectListItem>
    { new SelectListItem{Text="Male", Value="M"},
    new SelectListItem{Text="Female", Value="F"}}, "Please select")
  • #769512
    @Html.DropDownListFor(m => m.Gender, new List<SelectListItem>
    { new SelectListItem{Text="Male", Value="M"},
    new SelectListItem{Text="Female", Value="F"}}, "M")

    If you want to select by default as "M", then just replace with "Please select" to "M". So, by default it will select as M.

    Regards,
    V.M. Damodharan
    "Your talent will be worthless, when you have fear and tension."

  • #769663
    @Html.DropDownListFor(m => m.Gender, new List<SelectListItem>
    { new SelectListItem{Text="Male", Value="M",selectedValue},
    new SelectListItem{Text="Female", Value="F"}}, "Please select")

    or else

    you can assign the above drop down list to viewbag and on searching you change the selectListItem as you required and override the dropdownlist with the latest after you mentioned selectedValue

    Thanks!
    B.Ramana Reddy


  • Sign In to post your comments