Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC Html.ActionLink not rendering. Can you spot what I'm doing wrong?

I have an Html.ActionLink inside an IF statement in a partial view that is not rendering a hyperlink for me as expected. I placed a breakpoint on the line and confirmed that the IF statement is in fact being satisfied and the code inside of it is running. I also, just as an extra measure, tried substituting the Substring with a hard string. Any ideas why no link is rendering for me with this code?

<p>
    Resume (Word or PDF only): @if (Model.savedresume.Length > 0) { Html.ActionLink(Model.savedresume.Substring(19), "GetFile", "Home", new { filetype = "R" }, null); }
</p>
like image 279
Ryan Avatar asked Dec 18 '25 13:12

Ryan


1 Answers

Put an @ before Html.ActionLink(...)

Razor uses @ for a lot of different purposes, and most of the time it's fairly intuitive, but in cases like this it's easy to miss the problem.

@if (Model.savedresume.Length > 0) // This @ puts Razor from HTML mode 
                                   // into C# statement mode
{ 
    @Html.ActionLink( // This @ tells Razor to output the result to the page,
                      // instead of just returning an `IHtmlString` that doesn't
                      // get captured.
        Model.savedresume.Substring(19), 
        "GetFile", "Home", new { filetype = "R" }, 
        null) // <-- in this mode, you're not doing statements anymore, so you
              //     don't need a semicolon.
}
like image 120
StriplingWarrior Avatar answered Dec 20 '25 05:12

StriplingWarrior



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!