Reputation: 749
I can't get my Controller action method to get hit when I click 'Detail View' tab, or any tab for that matter. However, I can get the action method to hit if I comment out return false;
in the javascript section, but then my 'Selected' tab is not maintained.
<script type="text/javascript">
$(document).ready(function () {
$('.test').click(function () {
$('.test').removeClass('selected');
$(this).addClass('selected');
return false;
});
});
</script>
<div id="tabs" class="shadetabs">
<ul>
<li class="test selected">@Html.ActionLink("Overview", "Index", "Statistics")</li>
<li class="test">@Html.ActionLink("Detail View", "Detail", "Statistics")</li>
<li class="test">@Html.ActionLink("Trends", "Trends", "Statistics")</li>
</ul>
</div>
Upvotes: 0
Views: 940
Reputation: 1039548
By returning false you are canceling the default link action which is to send a request to your controller action. So if you do this you will need to manually send a request (AJAX?) and use the results from this request to modify the DOM. For example:
$('.test').click(function () {
$('.test').removeClass('selected');
$(this).addClass('selected');
$.ajax({
url: this.href,
success: function(result) {
// TODO: do something with the result here
}
});
return false;
});
Or you could do this without any javascript.
So for example you could write a custom HTML helper that will generate this menu:
public static MvcHtmlString MenuLink(
this HtmlHelper htmlHelper,
string linkText,
string actionName,
string controllerName
)
{
var routeData = htmlHelper.ViewContext.RouteData;
string currentAction = routeData.GetRequiredString("action");
string currentController = routeData.GetRequiredString("controller");
if (actionName == currentAction && controllerName == currentController)
{
return htmlHelper.ActionLink(
linkText,
actionName,
controllerName,
null,
new {
@class = "selected"
}
);
}
return htmlHelper.ActionLink(linkText, actionName, controllerName);
}
and then simply:
<ul>
<li>@Html.MenuLink("Overview", "Index", "Statistics")</li>
<li>@Html.MenuLink("Detail View", "Detail", "Statistics")</li>
<li>@Html.MenuLink("Trends", "Trends", "Statistics")</li>
</ul>
Upvotes: 2
Reputation: 3106
The code was good. But i think try to make li tag with unique id.
Upvotes: 0