How are internal members of my view model not available in the view?

I have some internal automatic properties in my view model, but my strongly typed view does not see them. All in one 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*@ 
+6
source share
1 answer

Views are compiled in a separate dynamically generated assembly by ASP.NET runtime. Therefore, you cannot use internal properties. Of course, you may have internal properties on your model, but as soon as you map them to the view model, there will be no problems, since you should always pass the view model to the view.

Conclusion: Always use only public properties on view models.

+11
source

Source: https://habr.com/ru/post/886823/


All Articles