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.Co unt > 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.Current Policy = policyCollections.rows.Fir stOrDefaul t(x => x.PolicyNumber.Equals(poli cyNumber.S ubstring(p olicyNumbe r.Length - 7)) && x.MasterCompany.Equals(mas terCompany ) && x.PolicyModule.Equals(poli cyModule)) ;
policydetailsModel.Modes = new SelectList(list,"Value","T ext");
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.
This is how i populate my dropdown
List<SelectListItem> list = new List<SelectListItem>();
if (policyCollections.rows.Co
{
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.Current
policydetailsModel.Modes = new SelectList(list,"Value","T
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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Here is my controller code :
PolicyDetailsModel policydetailsModel = new PolicyDetailsModel(); -- Model
SelectListItem item = null;
List<SelectListItem> list = new List<SelectListItem>();
if (policyCollections.rows.Co unt > 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(pol icyNumber. Length-7)+ policyNumb er.Substri ng(0,polic yNumber.Le ngth-7));
list.Add(item);
}
}
policydetailsModel.Current Policy = policyCollections.rows.Fir stOrDefaul t(x => x.PolicyNumber.Equals(poli cyNumber.S ubstring(p olicyNumbe r.Length - 7)) && x.MasterCompany.Equals(mas terCompany ) && x.PolicyModule.Equals(poli cyModule)) ;
policydetailsModel.Modes = new SelectList(list,"Value","T ext");
//if (policyCollections != null)
// policyCollections.rowCount = pageSize;
// return Json(policyCollections, JsonRequestBehavior.AllowG et);
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.Policy Module)
</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.
PolicyDetailsModel policydetailsModel = new PolicyDetailsModel(); -- Model
SelectListItem item = null;
List<SelectListItem> list = new List<SelectListItem>();
if (policyCollections.rows.Co
{
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
list.Add(item);
}
}
policydetailsModel.Current
policydetailsModel.Modes = new SelectList(list,"Value","T
//if (policyCollections != null)
// policyCollections.rowCount
// return Json(policyCollections, JsonRequestBehavior.AllowG
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
</dt>
<dd style="font: large">
@Html.DropDownList("Modes"
</dd>
What do I need to do in order for this to work.
ASKER
got it working. did exactly what you said.
Thanks
Thanks
Open in new window