How come internal members in my view model aren't accessible in the view?

我的未来我决定 提交于 2019-12-07 03:09:47

问题


I have some internal automatic properties in my view model but my strongly-typed view doesn't see them. Everything is in the same assembly, so why is this happening?

public class MyViewModel {
    public   int PublicProperty { get; set; }
    internal int InternalProperty   { get; set; }
}

.

@*My view*@
@model MyViewModel

@Model.PublicProperty

@Model.InternalProperty @*Causes compilation error*@

回答1:


Views are compiled in a separate dynamically generated assembly by the ASP.NET runtime. So you cannot use internal properties. You could of course still have internal properties on your model but once you map them to the view model there will be no problem as you should always be passing a view model to the view anyways.

Conclusion: always use only public properties on your view models.



来源:https://stackoverflow.com/questions/5809469/how-come-internal-members-in-my-view-model-arent-accessible-in-the-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!