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

1

6

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*@
Bittencourt answered 27/4, 2011 at 19:28 Comment(2)
what error do you see? you have PublicProperty as internal and InternalProperty as public, is that your intention.Boloney
it's a standard reference-not-defined-or-no-extensions-method-found compilation error. oops. I fixed the access modifier mismatch. thx!Bittencourt
C
13

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.

Chuvash answered 27/4, 2011 at 19:37 Comment(1)
i tried declaring public static darin{get;set;} in my class A and returning the model class via return view(new A()) in view i can access all the properties but unable to access darin ? any ideaGolding

© 2022 - 2024 — McMap. All rights reserved.