错误传递到字典中的模型项的类型为'System.Data.Entity.DynamicProxies

时间:2015-09-24 14:01:36

标签: c# asp.net entity-framework asp.net-mvc-4 dictionary

  

传递到字典中的模型项是类型的   'System.Data.Entity.DynamicProxies.User_3B687608CE2EBE077B3CAB0EA66E20DFC054F09C758B6620E58A15CACB9B74E5',   但是这个字典需要一个类型的模型项   'System.Collections.Generic.IEnumerable`1Models.UserPR]'。

这是我的代码:

控制器部分

public ViewResultBase ListUserPR(int idUser)
        {
            try
            {
                UserPRParameters parameters = new UserPRParameters();
                parameters.IdUser = idReuniao;
                base.SetViewBag(parameters);
                var model = this.UserPRServices.FindByUserPR(parameters);
                this.ViewBag.IdParameters = parameters.IdParameters;
                return this.ListView(model);
            }
            catch (ValidationException exception)
            {
                base.AddValidationErrors(exception);
                return base.PopUpSuccessView();
            }
        }

服务类

public IEnumerable<UserPR> FindByUserPR(UserPRParameters parameters)
        {
            try
            {
                var query = base.context.UsersPrs.AsQueryable();
                query = query.Where(x => x.IdUser== parameters.IdUser);

                return query
                    .Sort(parameters, x => x.IdUser)
                    .Paginate(parameters);
            }
            catch (Exception exception)
            {
                throw new Exception(this.context.Database.Connection.ConnectionString, exception);
            }
        }

部分视图_ListUserPR内部查看“管理”

@model IEnumerable<UserPR>

@{
    var idGrid = "grid" + this.ViewBag.IdParameters ?? string.Empty;

    var user = this.Model.FirstOrDefault();
    int? userId = 0;
    if (null != user)
    {
        userId= user.IdUser;
    }

    var grid = new IBM.Web.Helpers
        .WebGrid(
                //source: this.Model,
                id: idGrid, rowsPerPage: this.RowsPerPage,
                ajaxUpdateContainerId: idGrid
                );
    var columns = new WebGridColumn[] {
        grid.Column("XXXX", UserPRResources.xxx, style: "center"),
        grid.Column("XXXX", UserPRResources.xxx, style: "center"),
        grid.Column("XXXXX", UserPRResources.xxx, style: "center"),
        ....

查看管理和您的控制器

@model User
@{
    var id = "id" + Guid.NewGuid().ToString().Substring(0, 5);
    this.ViewBag.Title = UserResources.Management;

}
<div class="box-fields">
    @using (Ajax.BeginForm(
        this.DefaultActionCreate,
        "User",
        new DefaultAjaxOptions()
    ))
    {
        @Html.Partial("Validation")
          section">@Resources.ReuniaoResources.ListUser</h2>
            @Html.Partial("ListUserPR")

控制器

public ViewResultBase Management(int id)
        {
            var model = this.Service.Get(id);
            return base.SwitchView(model);
        }

服务

public User Get(int id)
        {
            return this.context.Users.Find(id);
        }

我有两张桌子

用户&gt;用户PR中的FK UserPR没有PK - 它是两个关系表(User和PR)的结果

1 个答案:

答案 0 :(得分:1)

我认为你可以通过

禁用代理创建
Configuration.ProxyCreationEnabled = false;
dbcontext构造函数中的