传递到字典的模型项的类型是“System.Data.Entity.DynamicProxies.Object [英] The model item passed into the dictionary is of type 'System.Data.Entity.DynamicProxies.Object

查看:1272
本文介绍了传递到字典的模型项的类型是“System.Data.Entity.DynamicProxies.Object的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有谁知道为什么我收到此错误:

 模型项目传递到字典类型
system.Data.Entity.DynamicProxies.Object_3E186F803589BF82895B10E08C
2A9AB68EFA619599418D6EB96585D14874CDC0',
但本词典需要类型的模型项目
System.Collections.Generic.IEnumerable`1 [MyApplication.Domain.Entities.Object]。

下面是我的code:

控制器:

 公开的ViewResult指数()
    {
        如果(User.Identity.IsAuthenticated)
        {
            的MembershipUser的currentUser = Membership.GetUser();
            GUID currentUserId =(GUID)currentUser.ProviderUserKey;
            如果(的currentUser = NULL&放大器;!&安培; currentUser.ProviderUserKey = NULL&放大器;!&安培; currentUser.IsApproved)
            {
                此举的结果=(从db.Moves招
                         其中,move.UserId == currentUserId
                         选择移动).FirstOrDefault();
                返回视图(结果);
            }
        }
        返回查看(db.Moves.ToList());
    }

查看:

  @model IEnumerable的< MovinMyStuff.Domain.Entities.Move>
@ {
ViewBag.Title =指数;
}@foreach(以型号VAR项)
{
< D​​IV ID =移动列表项>
    < UL>
        <立GT;
            <跨度类=动名>
                @ Html.DisplayFor(modelItem => item.StartCity)
                @ Html.DisplayFor(modelItem => item.StartState)
                @ Html.DisplayFor(modelItem => item.StartZip) -
                @ Html.DisplayFor(modelItem => item.EndCity)
                @ Html.DisplayFor(modelItem => item.EndState)
                @ Html.DisplayFor(modelItem => item.EndZip)
            < / SPAN>
        < /李>
        <立GT; @ Html.ActionLink(详细信息,详细信息,新{ID = item.MoveId},{新@class =按钮})LT; /李>
    < / UL>
< / DIV>
}


解决方案

这code:

 移动结果=(从db.Moves招
         其中,move.UserId == currentUserId
         选择移动).FirstOrDefault();
返回视图(结果);

试图返回的的结果。它的抱怨就是了的结果。所以,你可以只使用:

  VAR的结果=(从db.Moves招
               其中,move.UserId == currentUserId
               选择移动)。取(1).ToList();
返回查看(结果);

或者

 移动结果=(从db.Moves招
         其中,move.UserId == currentUserId
         选择移动).FirstOrDefault();
返回查看(新[] {结果});

我个人摆脱查询前pression这里,顺便说一句 - 这是使code比它需要的更加复杂。例如,您可以重写我的第一个选项为:

  VAR的结果= db.Moves.Where(移动=> move.UserId == currentUserId)
                      。取(1)
                      .ToList();
返回查看(结果);

甚至

 返回查看(db.Moves.Where(移动=> move.UserId == currentUserId)
              。取(1).ToList());

Does anyone know why I am getting this error:

The model item passed into the dictionary is of type
system.Data.Entity.DynamicProxies.Object_3E186F803589BF82895B10E08C
2A9AB68EFA619599418D6EB96585D14874CDC0', 
but this dictionary requires a model item of type 
'System.Collections.Generic.IEnumerable`1[MyApplication.Domain.Entities.Object]'.

Here is my code:

Controller:

        public ViewResult Index()
    {
        if (User.Identity.IsAuthenticated)
        {
            MembershipUser currentUser = Membership.GetUser();
            Guid currentUserId = (Guid)currentUser.ProviderUserKey;
            if (currentUser != null && currentUser.ProviderUserKey != null && currentUser.IsApproved)
            {
                Move result = (from move in db.Moves
                         where move.UserId == currentUserId
                         select move).FirstOrDefault();
                return View(result);
            }
        }
        return View(db.Moves.ToList());
    }

View:

@model IEnumerable<MovinMyStuff.Domain.Entities.Move>
@{
ViewBag.Title = "Index";
}

@foreach (var item in Model)
{
<div id="move-list-item">
    <ul>
        <li>
            <span class="move-name">
                @Html.DisplayFor(modelItem => item.StartCity) 
                @Html.DisplayFor(modelItem => item.StartState)
                @Html.DisplayFor(modelItem => item.StartZip) -
                @Html.DisplayFor(modelItem => item.EndCity) 
                @Html.DisplayFor(modelItem => item.EndState)
                @Html.DisplayFor(modelItem => item.EndZip)
            </span>
        </li>
        <li>@Html.ActionLink("Details", "Details", new { id = item.MoveId }, new { @class = "button" })</li>
    </ul>
</div>
}

解决方案

This code:

Move result = (from move in db.Moves
         where move.UserId == currentUserId
         select move).FirstOrDefault();
return View(result);

tries to return a single result. It's complaining that it wants a sequence of results. So you could just use:

var results = (from move in db.Moves
               where move.UserId == currentUserId
               select move).Take(1).ToList();
return View(results);

Or:

Move result = (from move in db.Moves
         where move.UserId == currentUserId
         select move).FirstOrDefault();
return View(new[] { result });

I would personally get rid of the query expression here, by the way - it's make the code more complicated than it needs to be. For example, you could rewrite my first option as:

var results = db.Moves.Where(move => move.UserId == currentUserId)
                      .Take(1)
                      .ToList();
return View(results);

Or even:

return View(db.Moves.Where(move => move.UserId == currentUserId)
              .Take(1).ToList());

这篇关于传递到字典的模型项的类型是“System.Data.Entity.DynamicProxies.Object的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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