0

This is my layout page:

<!doctype html>
<html lang="en">
<head>
    <meta charset='utf-8'>
    <title>@ViewBag.Title</title>
</head>
<body>       
    @Html.ActionLink("Home", "Index", "Home", null, new { @class = "selected"})
    @Html.ActionLink("Users", "Index", "User")
    @Html.ActionLink("Customers", "Index", "ProductCompany")

    @RenderBody()
</body>
</html>

I would like to modify actionlink "selected" class depending on the loaded view on the server side. I am looking for options available to do it.

Best regards

tereško
  • 58,060
  • 25
  • 98
  • 150
Dzendo
  • 199
  • 3
  • 13

1 Answers1

1

I think you could send the class name in the ViewBag and set it as below;

@Html.ActionLink("Home", "Index", "Home", null, new { @class = @ViewBag.myClass})
Kaf
  • 33,101
  • 7
  • 58
  • 78
  • Its only one a tag that will be selected. So you suggest to have: Html.ActionLink("Home", "Index", "Home", null, new { class = ViewBag.IsHomeSelectedClass}) Html.ActionLink("Users", "Index", "User", null, new { class = ViewBag.IsUserSelectedClass}) and in me views: HOME: ViewBag.IsHomeSelectedClass = "selected" USER: ViewBag.IsUserSelectedClass = "selected" Best regards – Dzendo Mar 07 '13 at 07:32
  • In your controller action, assign `@ViewBag.myClass = "selected"`. You can use many ViewBag properties for many different classes or you could use a single one for many ActionLinks. Depend on the requirement. – Kaf Mar 07 '13 at 11:27