Data not refreshing in MVC view with IE, but was in Chrome

I had some basic code binding to my model, and here is a beautiful snippet:

@using (Html.BeginForm("Manage", "Account", FormMethod.Post)) {
        <label>Select User</label>
        <select id="users" onchange="getUser()">
            <option selected="selected" value="0">Add User</option>
        </select>
        @Html.LabelFor(m => m.UserName)
        @Html.TextBoxFor(m => m.UserName)
       <input type="submit" id="save" value="Save" />
}

Yes, I used an HTML select list in MVC – don’t judge me.

I also had some jQuery calls. The first would populate the awesome (and easy to use) select control, and the second would be called when a user selected an item in my awesome (and easy to use) select list:

    function getUsers() {
        $.ajax({
            url: '@Url.Content("~/Account/Users")',
            method: "GET",
            dataType: 'json',
            async: false,
            success: function (data) {
                var options = $("#users");
                $.each(data, function (i, val) {
                    options.append($("<option />").val(val.UserId).text(val.UserName));
                });
            }
        });
    }
    function getUser() {
        $.ajax({
            url: '@Url.Content("~/Account/GetUser")',
            method: "GET",
            dataType: 'json',
            data: { 'userId': $("#users").val() },
            async: false,
            success: function (data) {
                $("#UserName").val(data.UserName);
            }
        });

With Chrome, everything was fine, and there was much happiness abounding at my stand up desk. But when using IE, the Sad Clown of Life paid a visit, and neither my select list nor my fields were updated after I would choose a record, update it, and click the save button.

The problem was that IE was caching my data. I understand, it was great data, but sometimes, you just need to let go. The key was adding the “cache: false” tag to the jQuery function, like so:

    function getUser() {
        $.ajax({
            url: '@Url.Content("~/Account/GetUser")',
  cache: false,
  …
        });

Success – I got data in IE.

Advertisements

,

  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: