简体   繁体   中英

Setting the Selected Value of a Dropdown list in the View from the Controller Action

I have 5 dropdowns, which are basically displaying "Select", "Yes" and "No". Initially they are set to "Select". Once he user chooses something, I am storing the data in a cookie (with Jquery) and eventually passing this to the ViewModel so that I can use it in the Controller.

When the user refreshes the page, I want these dropdown lists to be populated again with the value I have in the ViewModel.

At the moment I have the following code :-

Inside the View I have

<%: Html.DropDownList("FirstQuestYesNo", ViewData["FirstQuestYesNoData"] as SelectList, new { @class = "normalDropdowns" })%>

and in my controller I have the following :-

            var ddlYesNoData = new SelectList(new[]
                                      {
                                          new {ID="",Name=@Resources.GeneralTerms.GeneralTerms_Select},
                                          new {ID="Yes",Name=@Resources.GeneralTerms.GeneralTerms_Yes},
                                          new{ID="No",Name=@Resources.GeneralTerms.GeneralTerms_No},
                                      },
        "ID", "Name", 1);


        //Refresh the YesNo dropdown with the correct vals
        Dictionary<string, string> YesNoData = new Dictionary<string, string>();
        YesNoData.Add("FirstQuestYesNoData", viewModel.FirstQuestYesNoValue);
        YesNoData.Add("SecondQuestYesNoData", viewModel.SecondQuestYesNoValue);
        YesNoData.Add("ThirdQuestYesNoData", viewModel.ThirdQuestYesNoValue);
        YesNoData.Add("FourthQuestYesNoData", viewModel.FourthQuestYesNoValue);
        YesNoData.Add("FifthQuestYesNoData", viewModel.FifthQuestYesNoValue);

        foreach (var item in YesNoData)
        {
            ViewData[item.Key] = ddlYesNoData;
            if (item.Value != null)
            {
                var selected = ddlYesNoData.Where(x => x.Value == item.Value).First();
                selected.Selected = true;
            }
        }

So basically what I am doing is get the value of each dropdown from the viewModel, and then try to set that value inside my View. As a result of what I am doing, I am getting all the DropdownLists option as "Select" instead of the value inside my viewModel.

The problem is that I do not know how to "target" the specific DropDownList. How can I target the DropDown (in this case "FirstQuestYesNo") from the Controller using my code?

Thanks for your help and time.

In your controller action simply set the FirstQuestYesNo property to the corresponding value. For example:

ViewData["FirstQuestYesNo"] = "Yes"; // this value might come from a cookie

This will automatically preselect the option with value="Yes" . Obviously this value will come from the cookie. You don't need any foreach loops.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM