AFAIK,@Html.ActionLink()
帮助程序仅用于创建指向控制器的链接。但是,对于为其分配onclick()函数的问题,有一个非常简单的解决方法。
例子: (我假设为onclick事件JavaScript函数)
<script type="text/javascript">
function foo(string){
alert(string);
return false; //Very Important
}
</script>
所以后来在ActionLink的,你可以把:
@Html.ActionLink("Home", "", "", null, (ViewBag.SelectedPage == CurrentPageEnums.SelectedViewEnum.Home) ? new { @class = "current", @id = "Home",onclick="javascript:foo('foo2');" } : new { @id = "Home" })
注意return false
部分在JS功能。如果在点击ActionLink后不希望页面刷新,这一点非常重要。在ActionLink的ActionName和RouteValues中输入空字符串也会阻止它调用控制器。
至于你的ID的问题的第一部分,记得ID之前把@:
@Html.ActionLink("Home", "Index", "Home", null, (ViewBag.SelectedPage == CurrentPageEnums.SelectedViewEnum.Home) ? new { @class = "current", @id = "Home" } : new { @id = "Home" })
所以最后ActionLink的会是这个样子:
@Html.ActionLink("Home", "", "", null, (ViewBag.SelectedPage == CurrentPageEnums.SelectedViewEnum.Home) ? new { @class = "current", @id = "Home",onclick="javascript:somefunction()" } : new { @id = "Home" })
说完就把我不确定你是否可以指定: new {@id = "Home"}
所以要解决这个问题,我会建议像这样的:
@if(ViewBag.SelectedPage == CurrentPageEnums.SelectedViewEnum.Home)
{
@Html.ActionLink("Home", "", "", null, new { @class = "current", @id = "Home", onclick="javascript:alert('True');return false;" })
}
else
{
//Not sure what you would like to put in the else clause
@Html.ActionLink("Home", "", "", null, new { @class = "current", @id = "Home", onclick="javascript:alert('False');return false;"})
}
我不得不将类和id添加到双方,但它确实有效。我也必须在页面的actionlink中放入false来刷新页面。谢谢你的帮助。 – NNassar 2013-03-22 19:11:19