Link to home
Start Free TrialLog in
Avatar of pratikshahse
pratikshahse

asked on

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.
Avatar of Francisco Igor
Francisco Igor
Flag of Canada image

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

Avatar of pratikshahse
pratikshahse

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of Francisco Igor
Francisco Igor
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
got it working. did exactly what you said.

Thanks