我在部分 View 中的 IF 语句中有一个 Html.ActionLink,它没有按预期为我呈现超链接。我在该行上放置了一个断点,并确认 IF 语句实际上已被满足并且其中的代码正在运行。作为一项额外措施,我还尝试用硬字符串替换子字符串。任何想法为什么没有使用此代码为我呈现链接?
<p>
Resume (Word or PDF only): @if (Model.savedresume.Length > 0) { Html.ActionLink(Model.savedresume.Substring(19), "GetFile", "Home", new { filetype = "R" }, null); }
</p>
最佳答案
在 Html.ActionLink(...)
@
Razor 将 @
用于许多不同的目的,而且大多数情况下它相当直观,但在这种情况下很容易忽略问题。
@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.
}
关于asp.net-mvc - MVC Html.ActionLink 未呈现。你能看出我做错了什么吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22022386/