locked
Set dropdownlist selected value from session RRS feed

  • Question

  • User1052662409 posted

    Hi All,

    in razor I am binding country list. see the code below.

       @{var country_list = (List<CP.Models.Country_List>)Session["CountryList"];
                                foreach (var item in country_list)
                                {
                                    
                                    <option   value="@item.country_id">@item.country_name</option>
                                }
                            }

    It's binding perfectly.

    Now I have a county in session like Session["CountryName"]

    I want to dropdownlist to be selected in this session value..

    How to do that? Please suggest.

    Thursday, July 25, 2019 9:37 AM

Answers

  • User665608656 posted

    Hi demoninside,

    I want to dropdownlist to be selected in this session value..

    To achieve this , you just need to add a judgment when adding options to the loop, whether Session["CountryName"] value is equal to the current item.country_name, and if it is equal, add the selected attribute to the current option. 

    For more details , you could refer to the following code:

            <select>
                @{var country_list = (List<CP.Models.Country_List>)Session["CountryList"];
                    foreach (var item in country_list)
                    {
                        if (item.Name == Session["CountryName"].ToString())
                        {
                            <option value="@item.ID" selected>@item.Name</option>
                        }
                        else
                        {
                            <option value="@item.ID">@item.Name</option>
                        }
    
                    }
                }
            </select>

    Best Regards,

    YongQing.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, July 26, 2019 3:09 AM

All replies

  • User475983607 posted

    Use the standard Html.DropdownList() helper.

    https://odetocode.com/blogs/scott/archive/2013/03/11/dropdownlistfor-with-asp-net-mvc.aspx

    Thursday, July 25, 2019 11:00 AM
  • User1429838465 posted

    I try to keep my Views as code-free as possible and use a ViewModel to assist with creating the Dropdown list.

    I've posted a series here on the various dropdown lists you can create with MVC.

    https://www.danylkoweb.com/Blog/the-ultimate-guide-to-aspnet-mvc-dropdowns-JO

    Hope this helps,
    JD

    Thursday, July 25, 2019 12:52 PM
  • User665608656 posted

    Hi demoninside,

    I want to dropdownlist to be selected in this session value..

    To achieve this , you just need to add a judgment when adding options to the loop, whether Session["CountryName"] value is equal to the current item.country_name, and if it is equal, add the selected attribute to the current option. 

    For more details , you could refer to the following code:

            <select>
                @{var country_list = (List<CP.Models.Country_List>)Session["CountryList"];
                    foreach (var item in country_list)
                    {
                        if (item.Name == Session["CountryName"].ToString())
                        {
                            <option value="@item.ID" selected>@item.Name</option>
                        }
                        else
                        {
                            <option value="@item.ID">@item.Name</option>
                        }
    
                    }
                }
            </select>

    Best Regards,

    YongQing.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, July 26, 2019 3:09 AM