0

I have a drop down list as below which just shows a list of user names and their values:

@Html.DropDownListFor(m => m.SelectedUser,
                                  Model.UsersSelectList,
                                  "-- Select One --",
                                  new { onChange = "users_onchange();" })

and the javascript is as below which would call an action when an item is changed:

    function users_onchange() {

        var selectedUserId = $('#SelectedUser').val();

        if (selectedUserId == "") {
            selectedUserId = 0;
        }

        $('#EmailAccountsContainer')
            .load('/MyEntityController/EmailAccounts', { userId: selectedUserId });
   }

and the EmailAccountsContainer is just a div:

<div id="EmailAccountsContainer"></div>

and the EmailAccounts Action accepts a userId and returns a partial view displaying another drop down list.

The problem is that the call to the Action only happens after the first change to the Users drop down list. If a second user is selected, no call is made to the Action.

I have turned down the caching as well on the controller:

[OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]
public class MyEntityController : Controller

How to fix this?

tereško
  • 58,060
  • 25
  • 98
  • 150
The Light
  • 26,341
  • 62
  • 176
  • 258

2 Answers2

1

Bind you onchange event using on Event with Body tag

$(document).ready(function(){

$('body').on('change', '#SelectedUser', function(){
  var selectedUserId =$(this).val();
   if (selectedUserId == "") {
        selectedUserId = 0;
    }
   $('#EmailAccountsContainer')
        .load('/MyEntityController/EmailAccounts', { userId: selectedUserId });
 });
});
Murali Murugesan
  • 22,423
  • 17
  • 73
  • 120
0

The problem is the response is cached in your browser.

See this for how to solve this with Jquery:

Stop jQuery .load response from being cached

Community
  • 1
  • 1
Shai Aharoni
  • 1,955
  • 13
  • 25