I have an A - Z directory 'widget' that I have on every page. If the user is on the home page and they click something in the directory, I want to load up the directory page with the corresponding result loaded. But if the user is on the directory page and they click something, I want to asynchronously load the result without doing a page refresh.
The directory widget has links that point to the DirectoryResult action method on the GroupController, which would normally return a PartialView if they're on the directory page. But if they're not on the directory page, I redirect to the main Directory action method which returns a View and loads the entire page.
This is the code in question:
public ActionResult DirectoryResult(string search)
{
if (Request.IsAjaxRequest())
{
var groups = _groupService.GetGroupsBySearchExpression(search);
var premiumGroups = _groupService.FilterPremiumGroups(groups);
return PartialView(new FundDirectoryViewModel
{
Groups = groups,
PremiumGroups = premiumGroups
});
}
else
{
TempData[UIMessageDataKeys.FundDirectorySearch] = search;
return RedirectToAction("Directory", "Group");
}
}
I showed this to one of the guys in the office and his immediate response was "that's a hack!". I don't know whether to agree with him or not though, because I don't know any better way to do it.
For reference, this is the definition of the widget that exists on every page:
<div id="DirectoryList" class="directory-list">
<span>Fund Directory</span>
<% var letters = new [] { "A", "B", "C", "D", "E", "F", "G", "H", "I", ... }; %>
<% var current = (Model.Search.IsNotNullOrEmpty()) ? Model.Search : "A"; %>
<% foreach (var letter in letters) { %>
<span>
// use HtmlHelper extension to generate links as our system needs them
<%= Html.RouteActionLink("funddirectory", "DirectoryResult"
, letter
, (letter.ToLower() == current) ? new { @class = "active" } : new { @class = "" })%>
</span>
<%} %>
</div>
Is there a better way for me to determine whether I should return a PartialView or a View depending on the page the request comes from?