Internet Explorer does not refresh an ASP.NET MVC Ajax form correctly

I’ve come across a problem with Internet Explorer (I’ve looked at versions 8 and 10) when using a form called via Ajax in MVC. Basically, if you have a standard call to a Ajax.ActionLink such as this:

@Ajax.ActionLink("Edit Thingy", "Edit", "Thingy", new { id = thingy.Code }, new AjaxOptions { UpdateTargetId = "editMyThingy" })
 

Then IE appears to cache your form, so that the next time you view it the values in it are exactly the same as when you first loaded it up, making it look like nothing has been saved (it has been saved, it just looks like it hasn’t to the end user).

The obvious thing to try is to use the OutputCache attribute on your method like so:

[HttpGet]
[OutputCache(NoStore = true, Duration = 0, VaryByParam = "None")] 
public ActionResult Edit(string id)
{
 ...
}
 

However IE seems to ignore this. Eventually I got around the problem in a clunky way by forcing the ActionLink to call the method via a Post rather than a Get (see below). This seems to kick IE back into gear and doesn’t cache. Seems a bit hacky though, so if anyone has any other ideas please let me know!

@Ajax.ActionLink("Edit Thingy", "Edit", "Thingy", new { id = thingy.Code }, new AjaxOptions { UpdateTargetId = "editMyThingy", HttpMethod="Post"  })
 
[HttpPost]
public ActionResult Edit(string id)
{
 ...
}
 
Advertisements

Using MVC’s Html.RenderPartial in a form with the Razor view engine

I’ve found a little quirk with the Razor view engine (MVC3). Had me head-scratching for an hour or so, so I thought I should post it here! The following might only apply inside a partial view with Ajax, I’ve not had a look at non-Ajax or full views yet.

If your view/partial view has an Ajax form, and within that form a call to RenderPartial, like so:

@using (Ajax.BeginForm("MyAction", "MyController", new AjaxOptions() { /* various form options */}))
{
    <div>
       Some stuff
    </div>

    @{ 
         Html.RenderPartial("My Partial", Model); 
     }
}

Then although this looks fine you will get the following error message: CS1501: No overload for method ‘Write’ takes 0 arguments

To get it working simply surround the call to RenderPartial with a div tag, like below:

@using (Ajax.BeginForm("MyAction", "MyController", new AjaxOptions() { /* various form options */}))
{
    <div>
       Some stuff
    </div>

    <div> 
    @{ 
         Html.RenderPartial("My Partial", Model); 
     }
    </div>
}