MVC Html.ActionLink无法呈现.你能发现我做错了吗? [英] MVC Html.ActionLink not rendering. Can you spot what I'm doing wrong?

查看:55
本文介绍了MVC Html.ActionLink无法呈现.你能发现我做错了吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在部分视图的IF语句中有一个Html.ActionLink,它没有为我提供超链接.我在该行上放置了一个断点,并确认IF语句实际上已得到满足,并且其中的代码正在运行.我还作为一项额外措施,尝试用硬字符串替换Substring.知道为什么用此代码没有链接呈现给我吗?

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>

推荐答案

Html.ActionLink(...)

Razor将 @ 用于许多不同的目的,并且在大多数情况下是相当直观的,但是在这种情况下,很容易错过这个问题.

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

这篇关于MVC Html.ActionLink无法呈现.你能发现我做错了吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆