UPDATE <SOLVED!!>: Doggone JQuery! I finally got it to work. The issue was case-sensitivity with some of the keywords. Below is the JQuery code that finally worked:
Notice the case for 'value' and 'text'. Also note the case for 'itemData.Value' and 'itemData.Text'. The casing of the words 'value' and 'text' in these 2 contexts was the key. If I changed the case of ANY of them i did not get the desired results. The majority
of the sites I had reviewed before posting had ALL of those words ('text' and 'value') in lower-case. That did not work for me in either IE 9 or FF 10.
It loads initially just fine. I have one dropdown list, that, when an item is selected, will populate another dropdown list on post back. When an item is selected from the first dropdown list, it posts back to the action method in my controller just fine.
It even passes the correct text and values back to the client. The 2nd dropdown list just has not text for the items.
Thanks for all the replies! I've updated my original post to reflect the fact that I had to play around a bit in order to get things to work. None of the options I found online worked correctly for me. It all boiled down to a situation of needing to observe
JQuery case-sensitive issues.
jvboyd
Member
83 Points
47 Posts
cascading dropdown list text won't display <SOLVED>
May 08, 2012 09:31 PM|LINK
I have the following in my view:
<script type='text/javascript'> $('#DropDownCompany').change(function () { // var selectedCompany = $("#DropDownCompany").val(); var selectedCompany = $(this).val(); if (selectedCompany != null && selectedCompany != '') { $.getJSON('@Url.Action("testing")', { id: selectedCompany }, function (myData) { var facilitiesSelect = $('#DropDownFacility'); facilitiesSelect.empty(); $.each(myData, function (index, itemData) { facilitiesSelect.append($('<option/>', { Value:itemData.Value, Text: itemData.Text })); }); }); }; }); </Script>I have this in my view as well to declare my dropdown list:
@Html.DropDownList("DropDownFacility", (ViewData["DropDownFacilities"] as SelectList))I have the following in my controller:
public JsonResult testing(string id) { Dictionary<int, string> e = partReposit.getData(1013); SelectList myData = new SelectList(e, "Key", "Value"); return Json(myData, JsonRequestBehavior.AllowGet ); }I've skipped some extra code. But, using FireBug I can see the values for the dropdown list named: DropDownFacility
Yet, there are no labels in the list. The Html (via firebug) with labels would look like:
The Html with no labels looks like:
Any assistance would be greatly appreciated.
UPDATE <SOLVED!!>: Doggone JQuery! I finally got it to work. The issue was case-sensitivity with some of the keywords. Below is the JQuery code that finally worked:
$.each(myData, function (index, itemData) { facilitiesSelect.append($('<option/>', { value:itemData.Value, text: itemData.Text })); });Notice the case for 'value' and 'text'. Also note the case for 'itemData.Value' and 'itemData.Text'. The casing of the words 'value' and 'text' in these 2 contexts was the key. If I changed the case of ANY of them i did not get the desired results. The majority of the sites I had reviewed before posting had ALL of those words ('text' and 'value') in lower-case. That did not work for me in either IE 9 or FF 10.
postonoh
Member
498 Points
259 Posts
Re: cascading dropdown list text won't display <SOLVED>
May 08, 2012 10:14 PM|LINK
Do the dropdown list suppose load on window load or page load or are you trying to do a if
if (!IsPostBack)jvboyd
Member
83 Points
47 Posts
Re: cascading dropdown list text won't display <SOLVED>
May 09, 2012 12:00 AM|LINK
It loads initially just fine. I have one dropdown list, that, when an item is selected, will populate another dropdown list on post back. When an item is selected from the first dropdown list, it posts back to the action method in my controller just fine. It even passes the correct text and values back to the client. The 2nd dropdown list just has not text for the items.
postonoh
Member
498 Points
259 Posts
Re: cascading dropdown list text won't display <SOLVED>
May 09, 2012 01:57 AM|LINK
So you are doing the make, model, year a reference (Ajax CascadingDropDown)
http://www.asp.net/ajaxLibrary/AjaxControlToolkitSampleSite/Walkthrough/CCDWithDB.aspx
http://stackoverflow.com/questions/5497524/easiest-way-to-create-a-cascade-dropdown-in-asp-net-mvc-3-with-c-sharp
bhaskar.mule
Contributor
2270 Points
659 Posts
Re: cascading dropdown list text won't display <SOLVED>
May 09, 2012 02:07 AM|LINK
hi
ihope it will help you
http://csharpektroncmssql.blogspot.com/2011/12/cascading-dropdownl-lists-in-aspnet.html
Site:Rare technical solutions
jvboyd
Member
83 Points
47 Posts
Re: cascading dropdown list text won't display <SOLVED>
May 10, 2012 02:53 PM|LINK
Thanks for all the replies! I've updated my original post to reflect the fact that I had to play around a bit in order to get things to work. None of the options I found online worked correctly for me. It all boiled down to a situation of needing to observe JQuery case-sensitive issues.