asp.net-mvc - MVC Html.ActionLink 未呈现。你能看出我做错了什么吗?

标签 asp.net-mvc razor

我在部分 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/

相关文章:

jQuery 验证、ASP.NET MVC ModelState 错误(异步 POST)

c# - 使用 html5 将 byte[] 显示为 pdf

asp.net-mvc-3 - 如何在 MVC Razor View 中为下拉列表添加新选项?

regex - 正则表达式属性未按预期工作

jquery - 动态部分 View + jquery 表单劫持 + 客户端验证 = 不起作用

.net - Azure 上的 "You do not have permission to view this directory or page"Mvc 4 应用程序

c# - 在 ASP.NET MVC 中构建多 View 布局的首选方法?

c# - htmlunit .net MVC View 单元测试

asp.net-mvc - MVC 4 Razor - 动态命名 JavaScript 函数

asp.net - 使用asp.net身份更新用户角色