Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7735589
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T07:32:22+00:00 2026-06-01T07:32:22+00:00

I am working on an MVC3 application using the Razor syntax and I have

  • 0

I am working on an MVC3 application using the Razor syntax and I have been having some trouble with the AJAX.

The problem follows:
I have a DropDownList of categories defined like so:

 @Html.DropDownList("category_1", 
                 new   SelectList(LocalRuckusMVC.Code.StaticDictionaries.BusinessCategories,                                      
   "Value", "Text"), "Please Select A Category")

When a user selects a category the application makes an AJAX request to the server for a list of the appropriate subcategories for the selected category.

The jquery is set up like so:

$('#category_1').change(function () {
    $('#category_1').cascade({
        url: '@Url.Action("GetSubCats")',
        paramName: 'category_id',
        childSelect: $("#subCategory_1")
    });
});

and the cascade jquery extension:

    (function (jQuery) {
    $.fn.cascade = function (options) {
        var defaults = {};
        var opts = $.extend(defaults, options);

        return this.each(function () {
            jQuery(this).change(function () {
                var selectedValue = jQuery(this).val();
                var params = {};
                params[opts.paramName] = selectedValue;
                jQuery.getJSON(opts.url, params, function (items) {
                    var options = opts.childSelect.options;

                    opts.childSelect.empty();
                    window.console.log(params);
                    window.console.log(items);
                    jQuery.each(items, function (index, item) {
                        //window.console.log(index);
                        window.console.log(opts.childSelect);
                        window.console.log(item.Value);

                        var html = '<option value=\'' + item.Value + '\' >' + item.Text +    
                                     '</option>';
                        opts.childSelect.append(html);
                        window.console.log(html);


                    });
                });
            });
        });
    };
})(jQuery);

Everything works fine as far as the AJAX goes, I get the correct response and all that, the problem is that the user has to select a different category twice before the AJAX request is made.

I have done extensive debugging with firebug and fiddler and the change event doesn’t fire the first time a category is selected, or at least it does not appear to fire when looking at firebug or the chrome dev console.

Anybody have any thoughts??

All help is greatly appreciated.

Oops forgot to add the html select that this jquery method updates so here it is:

 <select id="subCategory_1" name="subCategory_1">
                </select>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T07:32:23+00:00Added an answer on June 1, 2026 at 7:32 am

    Aha I think its because you have another jQuery(this).change event defined on top of the existing change event.

    this is the original change event

    $('#category_1').change(function () {
    

    then inside the helper function you have

    jQuery(this).change(function () {
    

    which I think is asserting; only run the below code if its changed and the select hasnt changed again to trigger that.

    try the below revised version.

    $.fn.cascade = function (options) {
        var defaults = {};
        var opts = $.extend(defaults, options);
    
        return this.each(function () {
                var selectedValue = jQuery(this).val();
                var params = {};
                params[opts.paramName] = selectedValue;
                jQuery.getJSON(opts.url, params, function (items) {
                    opts.childSelect.empty();
                    window.console.log(params);
                    window.console.log(items);
                    jQuery.each(items, function (index, item) {
                        //window.console.log(index);
                        window.console.log(opts.childSelect);
                        window.console.log(item.Value);
    
                        var html = '<option value=\'' + item.Value + '\' >' + item.Text +
                                         '</option>';
                        opts.childSelect.append(html);
                        window.console.log(html);
                    });
                });
    
        });
    };
    

    also in your code you have

    var options = opts.childSelect.options;
    

    this has the same name as the function(options) and the variable is unused!! so I took it out 🙂 I’ve tested it and its working for me on the first select, hope it helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently working on my first MVC3 application at work (using the Razor view
I have been working on mvc3 application and trying to localize the same. I
I am working on an mvc3 application and having some problems with getting validation
I have a properly working ajax form in my MVC3 application except for one
I'm working on an MVC3 web application using the razor view engine. I'm looking
I'm working in a MVC3 application using Custom Membership Provider. The problem is when
I have created mvc3 application. where using EF to populate dropdownlist from tables. now
I've been working on writing my first MVC3 application for a little while and
I'm working on an MVC3 Razor web application which gets it's page decoration from
I'm working on Asp.net MVC3 web application. I'm having one page on which appx.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.