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

柔情痞子 提交于 2019-12-23 23:46:45

问题


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>

回答1:


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.
}


来源:https://stackoverflow.com/questions/22022386/mvc-html-actionlink-not-rendering-can-you-spot-what-im-doing-wrong

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!