DropDown text

In my MVC code I want to change the text of the dropdown based upon the values.

This is how i populate my dropdown

List<SelectListItem> list = new List<SelectListItem>();
            if (policyCollections.rows.Count > 0)
            {
               
                foreach (Policy policy in policyCollections.rows)
                {
                     item = new SelectListItem();
                    item.Text = policy.PolicyModule + " : " + policy.PolicyEffDate + "  " + policy.PolicyExpDate;
                    item.Value = item.Text;
                    list.Add(item);
                }

            }


policydetailsModel.CurrentPolicy = policyCollections.rows.FirstOrDefault(x => x.PolicyNumber.Equals(policyNumber.Substring(policyNumber.Length - 7)) && x.MasterCompany.Equals(masterCompany) && x.PolicyModule.Equals(policyModule));
            policydetailsModel.Modes = new SelectList(list,"Value","Text");

Definition for this action is

        public ActionResult Details(string policyNumber, string masterCompany, string policyModule)


the parameters are being passed from a url and everytime I change the parameters in the URL (manually)  I would like the text/value for the drop down to match the parameters that's being passed

If you can show me how we can get a specific text to show up in the dropdown based upon the text I pass then that will be great.

so this is what i want

string text =   ab :  12-31-2015 12-31-2016

If the same exact text is present in the dropdown then show me that text when the UI loads up.
pratikshahseAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

F IgorDeveloperCommented:
Use item.selected (boolean) to select an option. You can use a literal value ('SELECTEDVALUE') or getting from URL parameters

foreach (Policy policy in policyCollections.rows)
                {
                     item = new SelectListItem();
                    item.Text = policy.PolicyModule + " : " + policy.PolicyEffDate + "  " + policy.PolicyExpDate;
                    item.Value = item.Text;
                    // item is selected when matches a defined value
                    item.selected = (item.Value == 'SELECTEDVALUE');
                    list.Add(item);
                }

Open in new window

pratikshahseAuthor Commented:
that did not work.  item.selected ends up being true when the value matches but when the drop down loads it stillloads the first value  and not the selected value
F IgorDeveloperCommented:
Your selector could be associated to a model, then the model is setting the default value, using the model's value, bypassing your selection. Set the desired value in the model field before creating the control.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
pratikshahseAuthor Commented:
Here is my controller code :

 PolicyDetailsModel policydetailsModel = new PolicyDetailsModel();   -- Model
            SelectListItem item = null;

            List<SelectListItem> list = new List<SelectListItem>();
            if (policyCollections.rows.Count > 0)
            {
               
                foreach (Policy policy in policyCollections.rows)
                {
                     item = new SelectListItem();
                    item.Text = policy.PolicyModule + " : " + policy.PolicyEffDate + "  " + policy.PolicyExpDate;
                    item.Value = policy.PolicyModule + policy.MasterCompany + policy.PolicyNumber + policy.PolicySymbol;
                    item.Selected = (item.Value == policyModule+masterCompany+ policyNumber.Substring(policyNumber.Length-7)+policyNumber.Substring(0,policyNumber.Length-7));
                    list.Add(item);
                   
                }

            }
           

            policydetailsModel.CurrentPolicy = policyCollections.rows.FirstOrDefault(x => x.PolicyNumber.Equals(policyNumber.Substring(policyNumber.Length - 7)) && x.MasterCompany.Equals(masterCompany) && x.PolicyModule.Equals(policyModule));
            policydetailsModel.Modes = new SelectList(list,"Value","Text");
            //if (policyCollections != null)
            //    policyCollections.rowCount = pageSize;
            // return Json(policyCollections, JsonRequestBehavior.AllowGet);

            return View(policydetailsModel);

Here is my model code :

    public class PolicyDetailsModel
    {
        public Policy CurrentPolicy { get; set; }

        public SelectList Modes { get; set; }
    }

here is my view code :

                       <dt>
                            @Html.DisplayNameFor(model => model.CurrentPolicy.PolicyModule)
                        </dt>

                        <dd style="font: large">
                            @Html.DropDownList("Modes", Model.Modes, new { onchange = "ModeChange()" })
                        </dd>


What do I need to do in order for this to work.
pratikshahseAuthor Commented:
got it working. did exactly what you said.

Thanks
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.