【问题标题】:Why does a MVC view not have access to constants internal to the project为什么 MVC 视图无法访问项目内部的常量
【发布时间】:2012-05-25 12:58:45
【问题描述】:

我今天在工作中遇到的事情,我试图制作一个在类中使用常量的视图,以构造查询字符串。当常量是公共的但不是当常量是内部时,代码工作得很好。

这是模型:

public class LookupListModel
{
    internal const string ContactGroupTypeList = "ContactGroupType";

    // The below works fine
    //public const string ContactGroupTypeList = "ContactGroupType";
}

视图(正确的@using 位于顶部):

<li>@Html.ActionLink("Contact group types", "Lookup", new { list = LookupListModel.ContactGroupTypeList })</li>

【问题讨论】:

    标签: c# .net asp.net-mvc-3 constants access-modifiers


    【解决方案1】:

    视图被编译到另一个程序集中,这就是internal const 不起作用的原因。

    【讨论】:

    • 谢谢,以为会是这样。
    • 很公平。但是公共常量呢?那也不行。
    • @East of Nowhere,根据 OP,public const 确实有效。您的案例是内部类中的public const 吗?
    • 啊,不,原来我只是犯了一个简单的错误:我首先尝试在 @Model.MyConst 之类的视图中访问它 --- 忘记了 Model 是 instance ,当然所有的 const 都必须通过类名来访问。我的公共常量在我的视图中运行良好。 :-)
    • 有办法解决这个问题吗?例如,我可以强制将视图编译到同一个程序集中吗?
    猜你喜欢
    • 2011-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-24
    • 1970-01-01
    相关资源
    最近更新 更多