views:

213

answers:

3

I have a webpage with two radiobuttons and a dropdownlist as follows:

<div class="sectionheader">Course
    <div class="dropdown"><%=Html.DropDownList("CourseSelection", Model.CourseList, new { @class = "dropdown" })%> </div>         
    <div class="radiobuttons"><label><%=Html.RadioButton("CourseType", "Advanced", false )%> Advanced </label></div>
    <div class="radiobuttons"><label><%=Html.RadioButton("CourseType", "Beginner", true )%> Beginner </label></div>
</div>

The dropdownlist is strongly typed and populated with Model.CourseList (NB - on the first page load, 'Beginner' is the default selection and the dropdown shows the beginner course options accordingly)

What I want to be able to do is to update the DropDownList based on which radiobutton is selected i.e. if 'Advanced' selected then show one list of course options in dropdown, and if 'Beginner' selected then show another list of courses.

Edit - posted my own answer below to show solution that worked for me (finally!)

+2  A: 

Hey,

Continue to return your collection of selectlistitem; this translates to JSOn nicely, at least it should, as an array of objects that look like { text: "a", value: "1" } and you can loop through the array and recreate the list this way...

So it will work with strongly-typed objects. You just have to take the objects and construct the elements for the underlying drop down.

HTH.

Brian
@Brian. To use JSON, do I need to set return type of my action to JsonResult and also return Json(dropdownlistReository...)? Still trying to resolve this for past 4 hours and going around in circles...
Remnant
Check out this pretty good example: http://weblogs.asp.net/mehfuzh/archive/2009/04/28/using-of-json-result-in-asp-net-mvc-1-0.aspx
Brian
Also check this out: http://geekswithblogs.net/michelotti/archive/2008/06/28/mvc-json---jsonresult-and-jquery.aspx
Brian
Thanks for the links. I have checked them out and tried re-working my code based on them and code samples in jQuery in Action. I am still struggling to get this to work. See my edited post above.
Remnant
What specifically isn't working? Using firebug or IE 8 debugger can figure out whether data is coming back.
Brian
Ohh, wait you get a reference to .dropdown, which is a DIV. You block anything but a select... You want this statement: var dropdownList = $(".dropdown"); to be var dropdownList = $(".dropdown > select"); or var dropdownList = $("#CourseSelection"); I think that is the problem.
Brian
Thanks for the tip. I tried that but still no joy. I don't get any errors with my code so hard to pinpoint the issue. I just get no functionality i.e. I click the radiobuttons and dropdownlist does not update. Kinda frustrating as I have poured over and over the code to check for errors but all seems fine...though evidently it isn't...
Remnant
@Brian. SOLVED! - See revised post above. You were right on amending var dropdownlist. Also, replaced $.getJSON with $.post and amended action to ActionResult instead of JsonResult
Remnant
+1  A: 

The code I would like to call sits within my Controller:

public ActionResult UpdateDropDown(string courseType)
    {
        IDropDownList dropdownlistRepository = new DropDownListRepository();
        IEnumerable<SelectListItem> courseList = dropdownlistRepository.GetCourseList(courseType);
        return Json(courseList);
    }

Using examples provided in jQuery in Action, I now have the following jQuery code:

$('.radiobuttons input:radio').click(function() 
{
    var courseType = $(this).val(); //Get selected courseType from radiobutton
    var dropdownList = $("#CourseSelection"); //Ref for dropdownlist
    $.post("/ByCourse/UpdateDropDown", { courseType: courseType }, function(data) {
    $(dropdownList).loadSelect(data);
    });
 });

The loadSelect function is taken straight from the book and is as follows:

(function($) {
    $.fn.emptySelect = function() {
        return this.each(function() {
            if (this.tagName == 'SELECT') this.options.length = 0;
        });
    }

$.fn.loadSelect = function(optionsDataArray) {
    return this.emptySelect().each(function() {
        if (this.tagName == 'SELECT') {
            var selectElement = this;
            $.each(optionsDataArray, function(index, optionData) {
            var option = new Option(optionData.Text, optionData.Value);

                if ($.browser.msie) {
                    selectElement.add(option);
                }
                else {
                    selectElement.add(option, null);
                }
            });
        }
    });
}
})(jQuery);
Remnant
A: 

Hey Remnant, if you still see updates on this, can you check out my question http://stackoverflow.com/questions/3961167/updating-html-dropdownlists-using-jquery Im having the same problem as you had, but when i try to use your solution, i get nada.

John Stuart