2014-02-25 40 views
1

我有一个Html.ActionLink在IF语句中的部分视图中不按预期呈现超链接。我在线上放置了一个断点,并确认IF语句实际上得到满足,并且其中的代码正在运行。作为一个额外的措施,我也尝试用一个硬串替换子串。任何想法为什么没有链接为我呈现这个代码?MVC Html.ActionLink不呈现。你能发现我做错了什么吗?

<p> 
    Resume (Word or PDF only): @if (Model.savedresume.Length > 0) { Html.ActionLink(Model.savedresume.Substring(19), "GetFile", "Home", new { filetype = "R" }, null); } 
</p> 

回答

4

Html.ActionLink(...)

剃须刀采用了@之前有很多不同的目的@,大部分的时间是相当直观,但在这样的情况下,很容易错过的问题。

@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. 
} 
+1

非常感谢您解决我的问题和神奇的解释。我还没有意识到@在C#语句模式中有必要将结果输出到页面。尽管如此,仍然是完全合理的。 – Ryan