无法动态调度Html.ActionLink

And*_*son 56 c# asp.net-mvc html-helper

我有MVC3的问题

我正试图@Html.ActionLink()在我的博客项目中为标题生成链接.

ActionLink作品中使用常量字符串只是花花公子,但如果我使用Posts.Title(当前Post模型的标题被循环),我会得到以下异常:

CS1973: 'System.Web.Mvc.HtmlHelper<dynamic>' has no applicable method 
named 'ActionLink' but appears to have an extension method by that name.  
Extension methods cannot be dynamically dispatched. Consider casting  
the dynamic arguments or calling the extension method without the  
extension method syntax.
Run Code Online (Sandbox Code Playgroud)

Mik*_*keb 109

"考虑在没有扩展方法语法的情况下强制转换动态参数或调用扩展方法."

Posts.Title没有类型(因此是动态参数).

只需通过(字符串)Posts.Title投射它.

  • 由于这个答案仍然被新手发现和欣赏,我应该补充一点,通过指定模型的类型来强制键入视图也将解决这个问题,并且比演示你的参数更优雅. (2认同)