1

I'm currently writing a MVC C# application. Everything works just fine. I have a bit of functionality, where I fill up a Bootstrap modal box using an Ajax call, but the new page gets cached, despite my efforts to prevent that.

On my main page I have the following actionhandler to fill up the modal box:

function setExtraPermsOrAtts(appID){
    $.ajax({
        cache:false,
        url: "/Group/_modifyAppPermissionsOfGroup?appID=" + appID            
    }).done(function (result) {
        $("#addApplicationBody").html(result);
        $('#modal-add-application').modal('show');
    });
}

This gets caught by the following method:

    public ActionResult _modifyAppPermissionsOfGroup(int? appID = 0)
    {
        if (appID != 0)
        {
            ViewBag.selectedAppID = appID;
            Session["selectedGroupAppID"] = appID;
            ViewBag.modifyPermsLater = true;
        }
        Group group = (Group)Session["currentGroup"];
        return View(group);
    }

Another thing that might be relevant is the point where it 'goes wrong'. The resulting View in the Modalbox, has a few radio buttons, depending on the content of the database. There I do a razor statement to get the DB value:

bool valueOfRadButtons = BusinessLogic.Domain.GroupIS.getExistingGroupPermission( 
                 Model.LoginGroupID, myItem.ApplicationPermissionID).LoginPermissionState;

Does anyone know where I'm going wrong? Is it the Ajax call? The ActionResult method in the Controller? Or the inline razor statement? I know the data gets saved properly, cause I see so in the DB

ekad
  • 14,436
  • 26
  • 44
  • 46
GillesDV
  • 293
  • 1
  • 3
  • 16
  • `cache: false` adds a dummy time-stamp parameter to the URL to "ensure" the request is unique (and not cached). You could change it to a `post` (which never caches), or better yet add cache attributes to the `_modifyAppPermissionsOfGroup` server method to stop any caching. e.g. `[OutputCacheAttribute(VaryByParam = "*", Duration = 0, NoStore = true)]` – iCollect.it Ltd May 19 '15 at 08:33
  • I added the OutputCacheAttribute annotation to my ActionResult method, but that doesn't work. The valueOfRadButtons variable still gets cached. – GillesDV May 19 '15 at 08:53

1 Answers1

0

You can specify that the response shouldn't be cached like this:

Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1));
Response.Cache.SetValidUntilExpires(false);
Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetNoStore();

It can be more easy if you make your own attribute and decorate the action with it as shown here.

Community
  • 1
  • 1