I have a strange issue where an ActionLink won’t display. I have this code in my view:
@if (User.IsInRole("Allow Create")) {
Html.ActionLink("Add a new item", "Create");
}
I thought that maybe my roles and permissions weren’t set up correctly, but I went through the debugger, and sure enough the if-statement succeeds. It reaches the code to create the action link, and there are no errors.
But when the page loads, there is no link. It’s not hidden or anything either, it’s not in the source at all.
Anyone know what I’m doing wrong here?
you need an extra
@As for the explanation: the
@if (User.IsInRole("Allow Create")) {puts you inside a code block, and razor figures that you are just callingHtml.ActionLink(..)as if you were calling a function while not bothering about the returned result. Since you actually want to output the result of the Html.ActionLink as html, you are mixing code and text for which you need the@prefix.Just to illustrate, the following will have the same outcome as the code above:
Here is a nice Quick Reference