Abdul
Abdul

Reputation: 39

MVC 4 C# How to Retrieve Configuration Values in Controller to Bind Dropdown in View

I am trying to fill a drop down in my index view with the values I have listed in web.config.

<add key="DropdownValues" value="Value1, Value2, Value3" />

I want to grab Value1, Value2, and Value3 from the config and bring it into a dropdown list with a friendly display name for each value.

Value from the list will be used when submit is hit and a server is queried and returns results into a table.

I am not sure how to code the controller part of this to grab the values and send over to the index page (using viewbag?) Other examples involved creating a ViewModel but I want to be able to add to the values in the future without having to re-compile the app that's why I want it in the config.

Please show me an example as I am still new to MVC.

I assume in the view I would do something like: @Html.DropDownListFor(model => model.YourModelProperty, (IEnumerable)ViewBag.DropDownList, "---Select a value---")

Upvotes: 0

Views: 2299

Answers (2)

Travis Acton
Travis Acton

Reputation: 4440

Step 1 (In controller ActionResult Method): read the values from the config:

using System.Configuration;

string configvalue1 = ConfigurationManager.AppSettings["DropdownValues"];

Step 2 (In controller ActionResult Method): Parse them to an array by the comma and trim any space in front or behind the parsed text:

string[] values = configvalue1.Split(',').Select(sValue => sValue.Trim()).ToArray();

Step 3 (In controller ActionResult Method): Declare a selectlistItem List, Iterate through array to populate:

        List<SelectListItem> dropDowns = new List<SelectListItem>();
        for (int i = 0; i < values.Length; i++)
        {
            dropDowns.Add(new SelectListItem { Text = values[i], Value = values[i] });
        }

Step 4 (In controller ActionResult Method): Assign them in your ViewBag:

ViewBag.DropdownVals = dropDowns;

Step 5: (In your view) set a dropdown list that is bound to your viewbag item. You need to explicitly cast it to a IEnumerable SelectListItem object here or the razor engine will get angry!!!

@Html.DropDownList("YourElementName", (IEnumerable<SelectListItem>)ViewBag.DropdownVals, "--Choose Your Value--")

Full Controller:

    public ActionResult Index()
    {            
        string[] values = (ConfigurationManager.AppSettings["DropdownValues"]).Split(',').Select(sValue => sValue.Trim()).ToArray();
        List<SelectListItem> dropDowns = new List<SelectListItem>();
        for (int i = 0; i < values.Length; i++)
        {
            dropDowns.Add(new SelectListItem { Text = values[i], Value = values[i] });
        }
        ViewBag.DropdownVals = dropDowns;
        return View();
    }

Full View:

@{
    ViewBag.Title = "Index";
}
@Html.DropDownList("YourElementName", (IEnumerable<SelectListItem>)ViewBag.DropdownVals, "--Choose Your Value--")

Upvotes: 1

Jeff P.
Jeff P.

Reputation: 3004

Try doing something like this

Controller:

public ActionResult Index()
{
var GetWebConfigValue1 = System.Configuration.ConfigurationManager.AppSettings["WebConfigValue1"].ToString();

    ViewBag.WebConfigValue1 = GetWebConfigValue1;

    // Repeat the lines of code above to get more values

    return View();

}

View:

<select>
    <option selected="true" disabled="disabled">Choose a Value</option>
    <option value="@ViewBag.WebConfigValue1">@ViewBag.WebConfigValue1</option>
    <option value="@ViewBag.WebConfigValue2">@ViewBag.WebConfigValue2</option>
    <option value="@ViewBag.WebConfigValue3">@ViewBag.WebConfigValue3</option>
</select>

Upvotes: 1

Related Questions