The model item passed into the dictionary is of type 'System.Data.Entity.DynamicProxies.Object

北城余情 提交于 2019-11-30 17:33:45

问题


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>
}

回答1:


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());


来源:https://stackoverflow.com/questions/11238905/the-model-item-passed-into-the-dictionary-is-of-type-system-data-entity-dynamic

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!