Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# MVC-如何在Html.ActionLink中动态设置@class_C#_Asp.net Mvc 3_Razor - Fatal编程技术网

C# MVC-如何在Html.ActionLink中动态设置@class

C# MVC-如何在Html.ActionLink中动态设置@class,c#,asp.net-mvc-3,razor,C#,Asp.net Mvc 3,Razor,如何在ActionLink中动态设置@class 我想做什么 @Html.ActionLink("Pricing", "Index", "Pricing", new { PageIndex = 2, @(ViewBag.PageIndex == 2 ? @class="" : @class="ActiveMenuItem" )}, null) 但是运行时会因为我的语法而崩溃。假设您希望“class”是一个HTML属性,“PageIndex”是一个动作参数,您可以这样做: <a href=

如何在ActionLink中动态设置@class

我想做什么

@Html.ActionLink("Pricing", "Index", "Pricing", new { PageIndex = 2, @(ViewBag.PageIndex == 2 ? @class="" : @class="ActiveMenuItem" )}, null)
但是运行时会因为我的语法而崩溃。

假设您希望“class”是一个HTML属性,“PageIndex”是一个动作参数,您可以这样做:

<a href="@Url.Action("Index", "Pricing")?PageIndex=2" class="@(ViewBag.PageIndex == 2 ? "ActiveMenuItem" : "")">Pricing</a>

坚持“class”是指html属性吗?“页面索引”是动作参数吗?如果这些是不同的,那么我可能需要调整我的answer@musefan-是的,你是对的。我将更新您的答案,以反映我最终实际使用的内容。它非常接近你的建议,所以你保持信用。我已经更新,以显示如何仍然可以使用ActionLink,如果需要的话
@Html.ActionLink("Pricing", "Index", "Pricing", new {PageIndex = 2}, new {@class = ViewBag.PageIndex == 2 ? "" : "ActiveMenuItem"})
@Html.ActionLink("Pricing", "Index", "Pricing", 
new { PageIndex = 2, @class = (ViewBag.PageIndex == 2)? "" : "ActiveMenuItem" },
 null)