Is there a better way to track the selected item than how I do it in the code below which implements a row of navigation links.
@Html.ActionLink(
"PreApproval",
"Summary",
new { mode = "preapproval" },
new { @class = Model.Mode == "preapproval" ? "selected" : "notselected" }) |
@Html.ActionLink(
"ActionNeeded",
"Summary",
new { mode = (string)null },
new { @class = string.IsNullOrWhiteSpace(Model.Mode) ? "selected" : "notselected" }) |
...
Should I try to encasulate the functionality of menu navigation or is this a standard approach?
You could encapsulate this into a little
Func<bool, string>helper, but it depends on what it buys you. Having it the way you have it in your question keeps the decision closer to the link, and is clearer to read & understand, without having to jump to a helper method during debugging.If you had 20 or more links like this, I might refactor. That way you could update in 1 place if, for example, your css class names changed from “selected/notselected” to “current/notcurrent” or something else.
Another thing, in MVC4, if the CSS class name is a null or empty string, Razor won’t render the class attribute at all (will omit it). That way you could just put the “selected” class on the selected link(s), and get rid of the “notselected” class.