为什么视图模型中无法访问我的视图模型中的内部成员? [英] How come internal members in my view model aren't accessible in the view?

查看:103
本文介绍了为什么视图模型中无法访问我的视图模型中的内部成员?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的视图模型中有一些内部自动属性,但我的强类型视图看不到它们。

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*@


推荐答案

视图是单独动态编译的由ASP.NET运行时生成的程序集。因此,您不能使用内部属性。当然,您仍然可以在模型上保留内部属性,但是一旦将它们映射到视图模型就不会有问题,因为无论如何您始终应该将视图模型传递给视图。

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.

这篇关于为什么视图模型中无法访问我的视图模型中的内部成员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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