Having some odd behavior I’d love some feed back on as I can’t seem to find any details on this sort of thing anywhere…
Setup:
Using ASP.NET MVC 4 and VS 2010, I have created a corporate directory application with a search page and a details page that displays a corporate tree based on the search selection.
The search page is very basic and using ajax.beginform it returns a list of people back based on the criteria selected. After clicking on the person you get a “tree” showing that individual, their supervisor and any subordinates.
Problem
The user searches for someone, clicks on their result, sees the tree. When they click the back button (with or without performing any other action on the tree view) they are returned to the search screen. The criteria they entered is there but the results are not (as I would expect). If they click search or hit the enter key, the user then receives a 404 page not found error…
What I’ve Tried
From the things I’ve read, I’ve been focusing on possible solutions revolving around the cache, I’ve tried expiring the page or setting the page for no-cache but that has not seemed to resolve the issue.
CODE
Search View:
@using (Ajax.BeginForm("PersonList", "Home", new AjaxOptions { UpdateTargetId = "results", InsertionMode=InsertionMode.Replace }))
{
@Html.LabelFor(model => model.LastName)
@Html.TextBoxFor(model => model.LastName)<br />
@Html.LabelFor(model => model.FirstName)
@Html.TextBoxFor(model => model.FirstName)<br />
<input type="submit" value="Search" />
<div id="results">
</div>
}
Block that gets plopped in for the search results. This is a partial view that is reused for the tree view as well. That is why you’ll see that the link at the top of the block is in a conditional. If the link is to be used from the search page, it’s an html link that forces a full page change. If you’re already on the tree view though, navigation is done via ajax. The 404 error I’m receiving occurs without navigating at all on the tree view page:
<div id="p@{ Html.DisplayFor(model => model.Id); }" class="personCard">
<img class="personImg" id="img@{ Html.DisplayFor(model => model.Id); }" src="@Url.Content("~/Photos")/@Html.DisplayFor(model => model.Id)" />
<div>
@if(ViewBag.IsForSearch == null) {
@Ajax.ActionLink(this.Model.FullName, "PersonHierarchy", new { id = this.Model.Id }, new AjaxOptions { HttpMethod = "POST", InsertionMode = InsertionMode.Replace, UpdateTargetId = "tree" })
} else {
@Html.ActionLink(this.Model.FullName, "Person", new { id = this.Model.Id })
}
@if(this.Model.TeamSize > 0) {
@: (
@Html.DisplayFor(model => model.TeamSize)
@:)
}
<br />
@Html.DisplayFor(model => model.Title)<br />
@Html.DisplayFor(model => model.MailLocationCode), @Html.DisplayFor(model => model.WorkNumber) @Html.DisplayFor(model => model.ExtensionForDisplay)<br />
@if (this.Model.CellNumber.Trim() != string.Empty)
{
@:Cell:
@Html.DisplayFor(model => model.CellNumber)
@:<br />
}
@Html.DisplayFor(model => model.EmailAddress)
</div>
</div>
Response from Server:
Server Error in ‘/’ Application.
The resource cannot be found. Description: HTTP 404. The resource you
are looking for (or one of its dependencies) could have been removed,
had its name changed, or is temporarily unavailable. Please review
the following URL and make sure that it is spelled correctly.Requested URL: /
——————————————————————————– Version Information: Microsoft .NET Framework Version:4.0.30319;
ASP.NET Version:4.0.30319.272
EDIT:
This appears to be specific to IE 9 (in all modes). This behavior does not happen in Chrome and the search form resubmits perfectly after using the back button…
OK…I seem to have found the answer:
To recap, specifically in IE 9 when using ASP.NET MVC Ajax, using the back button to navigate back to a page that has a ajax form on it and trying to submit that form results in a 404 error.
I initially looked into various cache solutions to try to force IE to realize that the page needed to be “reloaded” (even though Chrome and other browsers had no issue with making the form work again when using the back button). This apparently was the correct path to go down but unfortunately I didn’t get far enough. The solution ends up being setting the OutputCache for the controller action (or to the entire controller) to:
Adding that annotation to the action in the controller or to the controller class as a whole resolved the problem…thanks IE…
For further information on this, I refer to my source:
http://dougwilsonsa.wordpress.com/2011/04/29/disabling-ie9-ajax-response-caching-asp-net-mvc-3-jquery/
Thanks to everyone who tried to help.