剃刀代理类型的错误。 System.Data.Entity.DynamicProxies [英] Razor proxy type error. System.Data.Entity.DynamicProxies

查看:244
本文介绍了剃刀代理类型的错误。 System.Data.Entity.DynamicProxies的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一类用户,然后另一种类型UserSpecial一些特殊的用户属性。
我通过它在剃刀分部方法类来创建它期望类型用户的对象特殊的UserSpecial形式,但我得到一个错误。

  @model用户
    @using(Html.BeginForm())
    {        @ Html.ValidationSummary(真)
        <&字段集GT;
            @ Html.Partial(../ UserSpecial / _CreateOrEdit,Model.UserSpecial)
            < p =类明确提出>
                <输入类型=提交值=注册/>
            &所述; / P>
        < /字段集>
    }
< / DIV>

错误我得到 -

传递到字典中的模型产品类型'System.Data.Entity.DynamicProxies.User_AC9DED50C9495788046D6BFA3B90DDFC6AD2884157CF23C91FCB3B7A55F70B18',但本词典需要类型的模型项目UserSpecial。

我究竟做错了什么?

从我的控制,我传递,我已经存储在会话状态的当前用户对象。

控制器 -

 公众的ActionResult寄存器()
        {
            返回查看(userRepository.Current);
        }

下面是电流型的用户

示范 -

 公共部分类用户
    {
        公共用户()
        {
        }        公众诠释用户名{搞定;组; }
        公共字符串名字{获得;组; }
        公共字符串名字{获得;组; }
        公共字符串性别{搞定;组; }
        公众的System.DateTime CREATEDATE {搞定;组; }
        公共字符串电子邮件{获得;组; }
        公共字符串的家乡{搞定;组; }
        公共短的UserType {搞定;组; }        公共虚拟UserSpecial UserSpecial {搞定;组; }
    }

示范宣言_CreateOrEdit是

  @model UserSpecial


解决方案

不知道是什么 userRepository.Current 是,但它似乎不正确/急切地加载 UserSpecial 属性。你为什么不使用视图模型?你为什么要传递域实体模型,您的看法?这是不好的做法。应该定义仅包含是由您的视图要求,然后在你的控制器动作映射您的域模型,将被传递给视图相应的视图模型之间的数据视图模型。

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 expects an object of type User Special but i get an error.

@model User
    @using (Html.BeginForm())
    { 

        @Html.ValidationSummary(true)
        <fieldset>
            @Html.Partial("../UserSpecial/_CreateOrEdit", Model.UserSpecial)
            <p class="submit clear">
                <input type="submit" value="Register" />
            </p>
        </fieldset>
    }
</div>

Error i get -

The model item passed into the dictionary is of type 'System.Data.Entity.DynamicProxies.User_AC9DED50C9495788046D6BFA3B90DDFC6AD2884157CF23C91FCB3B7A55F70B18', but this dictionary requires a model item of type 'UserSpecial'.

What am i doing wrong here?

From my controller i just pass the current User Object that i have stored in the session state.

Controller -

 public ActionResult Register()
        {
            return View(userRepository.Current);
        }

Here Current is of type "User"

Model -

public partial class User
    {
        public User()
        {
        }

        public int UserID { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Sex { get; set; }
        public System.DateTime CreateDate { get; set; }
        public string Email { get; set; }
        public string HomeTown { get; set; }
        public short UserType { get; set; }

        public virtual UserSpecial UserSpecial { get; set; }
    }

Model Declaration for _CreateOrEdit is

@model UserSpecial

解决方案

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.

这篇关于剃刀代理类型的错误。 System.Data.Entity.DynamicProxies的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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