On select change event - Html.DropDownListFor?

Give both dropdowns unique IDs using the HTTPAttributes field.

Up vote 1 down vote favorite 3 share g+ share fb share tw.

I have two dropdownlist. The selected value from the first one loads the other. How do I do that when I have the helper methods in a controller?

@using (Html.BeginForm()) { Select a District: @Html. DropDownListFor(m => m. DistrictId, ViewData"DMManagers" as IEnumerable, "Select One") Select a TM: @Html.

DropDownListFor(m => m. TMId, ViewData"TMManagers" as IEnumerable, "Select One") } private void LoadDistrictManagers() { var _DMS = (from c in SessionHandler.CurrentContext. ChannelGroups join cgt in SessionHandler.CurrentContext.

ChannelGroupTypes on c. ChannelGroupTypeId equals cgt. ChannelGroupTypeId where cgt.

Name == "District Manager" select new { c. ChannelGroupId, c. Name }).

OrderBy(m => m. Name); ViewData"DMManagers" = new SelectList(_DMS, "ChannelGroupId", "Name"); } private void LoadTerritoryManagers(int districtId) { var _TMS = (from c in SessionHandler.CurrentContext. ChannelGroups join cgt in SessionHandler.CurrentContext.

ChannelGroupTypes on c. ChannelGroupTypeId equals cgt. ChannelGroupTypeId where cgt.

Name == "Territory" && c. ParentChannelGroupId == districtId select new { c. ChannelGroupId, c.

Name }). OrderBy(m => m. Name); ViewData"TMManagers" = new SelectList(_TMS, "ChannelGroupId", "Name"); } public ActionResult SummaryReport() { DistrictManagerModel model = new DistrictManagerModel(); LoadDistrictManagers(); return View("AreaManager", model); } asp.

Net-mvc-3 link|improve this question edited Apr 25 '11 at 21:17Karl von Moor2,931622 asked Apr 25 '11 at 21:12bladerunner99214 78% accept rate.

Give both dropdowns unique IDs using the HTTPAttributes field: @Html. DropDownListFor(m => m. DistrictId, ViewData"DMManagers" as IEnumerable, "Select One", new {@id="ddlDMManagers"}) 2nd dropdown should be initialized as an empty list: @Html.

DropDownListFor(m => m. TMId, Enumerable.Empty(), new {@id="ddlTMManagers"}) If you don't mind using jQuery ajax to update the 2nd dropdown when a 'change' event is triggered on the 1st dropdown: $(function() { $('select#ddlDMManagers'). Change(function() { var districtId = $(this).val(); $.

Ajax({ url: 'LoadTerritoryManagers', type: 'POST', data: JSON. Stringify({ districtId: districtId }), dataType: 'json', contentType: 'application/json', success: function (data) { $. Each(data, function (key, TMManagers) { $('select#ddlTMManagers').

Append('Select One'); // loop through the TM Managers and fill the dropdown $. Each(TMManagers, function(index, manager) { $('select#ddlTMManagers'). Append( '' + manager.

Name + ''); }); }); } }); }); }); Add this class to your controller namespace: public class TMManager { public int Id {get; set;} public string Name {get; set;} } You will need to update your controller action, LoadTerritoryManagers(), to respond to the ajax request and return a JSON array of {Id,Name} objects. HttpPost public ActionResult LoadTerritoryManagers(int districtId) { var _TMS = (from c in SessionHandler.CurrentContext. ChannelGroups join cgt in SessionHandler.CurrentContext.

ChannelGroupTypes on c. ChannelGroupTypeId equals cgt. ChannelGroupTypeId where cgt.

Name == "Territory" && c. ParentChannelGroupId == districtId select new TMManager(){ Id = c. ChannelGroupId, Name = c.

Name }). OrderBy(m => m. Name); if (_TMS == null) return Json(null); List managers = (List)_TMS.ToList(); return Json(managers); }.

Thanks for your help..its not populating the TMs. Could it be this line in the jquery? $.

Post('/LoadTerritoryManagers', { districtId: districtId }, function (TMManagers)...The app builds fine but when I change the District, the TMs' is not populated. – bladerunner Apr 25 '11 at 22:34 I think so, and I can help you further in an hour. If you want, try putting a breakpoint in your LoadTerritoryManagers() function and see if the function is even reached.

If not, then the problem is in the $.post() like you suspect. If it is reached, I would use Chrome Developer tools or Firebug to add a breakpoint to the $.post() line and step through the code to see what is happening.. – BumbleB2na Apr 25 '11 at 22:48 I just tweaked this line: $. Post('LoadTerritoryManagers', {districtId: districtId }, function (TMManagers) {...Now whenever I change the district value, it keeps adding Select One to the second dropdown list... – bladerunner Apr 25 '11 at 22:52 Good it's close.

I updated the line for the $.each() statement. Try that. If that doesn't work, try TMManagers.

D instead of TMManagers as in: $. Each(TMManagers. D, function(..... – BumbleB2na Apr 25 '11 at 23:00 when I try the one you edited, the dropdownlist get filled with undefined...with the one above, I changed $.

Each(TMManagers. D, function(... this doesn't work. I think you are very close to make this work..thanks again for your help.

– bladerunner Apr 25 '11 at 23:04.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions