MVC控制器未受到攻击 [英] MVC Controller Not Getting Hit
问题描述
当我单击详细信息视图"选项卡或任何与此相关的选项卡时,我无法获得控制器动作方法的提示.但是,如果我在javascript部分中将return false;
注释掉,则可以找到要点击的操作方法,但是我的选择"标签未得到维护.
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>
推荐答案
通过返回false,您正在取消默认的链接操作,该操作将向控制器操作发送请求.因此,如果执行此操作,则需要手动发送请求(AJAX?),并使用该请求的结果来修改DOM.例如:
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;
});
或者您可以在没有任何JavaScript的情况下执行此操作.
Or you could do this without any javascript.
例如,您可以编写一个自定义HTML帮助器将会生成此菜单:
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);
}
然后简单地:
<ul>
<li>@Html.MenuLink("Overview", "Index", "Statistics")</li>
<li>@Html.MenuLink("Detail View", "Detail", "Statistics")</li>
<li>@Html.MenuLink("Trends", "Trends", "Statistics")</li>
</ul>
这篇关于MVC控制器未受到攻击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!