Kwasi Denkyira

Kwasi Denkyira

  • NA
  • 41
  • 1.8k

set selected value of one dropdown to another dropdown mvc

Apr 24 2017 3:35 PM
I have  using MVC5 and have two dropdowns.When users select an item in the first dropdown I want the second dropdown to show the related item if their their ID's match. Below is my code
public JsonResult LoadTest(int id)
{
try
{
var cd = (from h in Purchases
from s in h.Schedules
where h.Id == id || h.TOId == h.Admin.TOId
&& !h.Inactive
&& !s.Finalized
&& !s.NotRequired
)
orderby s.DateSort
select h).Distinct();
var cSelectItems = cd
.Distinct()
.Select(item => new SelectListItem
{
Text = item.Number + "/" + item.Name,
Value = item.Id.ToString( )
});
ViewData["Class"] = cdrlSelectItems;
foreach (var t in cd)
{
if(t.TOId > 0)
{
var wto = (from h in Purchases
from s in h.Schedules
where h.Id == id && h.TOId == h.Admin.TOId
&& !h.Inactive
&& !s.Finalized
&& !s.NoLongerRequired
)
orderby s.DateSort
select h).Distinct();
var cWdtoSelect = wto
.Distinct()
.Select(item => new SelectListItem
{
Text = item.Number + "/" + item.CName,
Selected = item.Id == t.TOId,
Value = item.Id.ToString( )
});
return Json(cWdtoSelect.ToList(), JsonRequestBehavior.AllowGet);
}
}
return Json(cSelectItems.ToList(), JsonRequestBehavior.AllowGet);
}
catch (Exception ex)
{
throw;
}

Answers (1)