Home how to set selected value in dropdown based on another value
Reply: 1

how to set selected value in dropdown based on another value

Kewin Björk Nielsen
Kewin Björk Nielsen Published in 2017-11-14 19:02:36Z

are there a away i can set the selected value on my dropdown based on the key value

<add key="ReCreateSoldPart" value="false" />from my appsetting

     <label for="autoCreateSoldPart" datalabel="mydropdown">Auto genopret solgt del&nbsp;&nbsp;
        <select name="autoCreateSoldPart">
            <option value="true">Ja</option>
            <option value="false"Nej</option>

Lolu Omosewo
Lolu Omosewo Reply to 2017-11-14 19:38:55Z

To select an option in razor, do it as follows; Using the @Html helper;

                @Html.Label("Auto genopret solgt del&nbsp;&nbsp;", new {@for= "autoCreateSoldPart"})
                    var lstSelect = new List<SelectListItem>()
                        new SelectListItem() { Text="Ja",Value="true"  },
                        new SelectListItem() { Text="Ja",Value="false"  }

                    lstSelect.ForEach(o =>
                        if (o.Value== System.Configuration.ConfigurationManager.AppSettings["ReCreateSoldPart"])
                            o.Selected = true;

                @Html.DropDownList("autoCreateSoldPart", lstSelect,new { @class = "form-control" })

Use the @Html.DropDown instead. Check the selectListItem property value of each item in List, if it matches your appsetting value, set the selected property to true

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.315596 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO