Razor proxy type error. System.Data.Entity.DynamicProxies

前端 未结 2 394
北恋
北恋 2021-01-15 12:59

I have a class User and then another Type UserSpecial with some special user properties. I pass it in razor to the partial method class to create the UserSpecial form which

相关标签:
2条回答
  • The solution for this issue is pretty simple:

    You should just use a wrapper class (view model) as Darin suggested.

    So for example:

    (entity domain model):

    public class MyEntityModel
    {
        public int Id { get; set; }
        public String Name { get; set; }
    }
    

    => put it in a ViewModel (just a stupid wrapper) should result in this

    public class MyViewModel
    {
        public MyEntityModel MyEntityModel { get; set; }
    }
    

    Now, in the view, u should be able to access the "name" property by doing this

    <div>
        The entity object's name is "@model.MyEntityModel.Name"
    </div>
    

    (notice you should not use @model.Name!)

    0 讨论(0)
  • 2021-01-15 13:18

    No idea what userRepository.Current is but it seems that it doesn't correctly/eagerly load the UserSpecial property. Why don't you use view models? Why are you passing domain entity models to your views? That's bad practice. You should define view models that contain only the data that's required by your view and then in your controller action map between your domain models and the corresponding view model which will be passed to the view.

    0 讨论(0)
提交回复
热议问题