2012-06-28 3 views
4

Кто-нибудь знает, почему я получаю эту ошибку:

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]'. 

Вот мой код:модель элемент передается в словарь типа «System.Data.Entity.DynamicProxies.Object

контроллера :

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

Вид:

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

ответ

4

Этот код:

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

пытается вернуть единственный результат. Хочется жаловаться, что ему нужна результатов. Таким образом, вы могли бы просто использовать:

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

Или:

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

Я бы лично избавиться от выражения запроса здесь, кстати, - это сделать код более сложным, чем это должно быть. Например, вы могли бы переписать мой первый вариант, как:

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

Или даже:

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

Я получаю эту ошибку, когда я добавить .Снять (1) - Ошибка Не удается неявно преобразовать type 'System.Linq.IQueryable ' to 'MovinMyStuff.Domain.Entities.Move'. Явное преобразование существует (вы пропускаете листинг?) –

+0

Вместо .Take (1) я бы выполнил .TakeWhile()? –

+0

Вместо того, чтобы принимать (1), как бы я взял столько, сколько создал пользователь? –

Смежные вопросы

 Смежные вопросы