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

前端 未结 1 1796
南方客
南方客 2021-01-13 05:03

Does anyone know why I am getting this error:

The model item passed into the dictionary is of type
system.Data.Entity.DynamicProxies.Object_3E186F803589BF82         


        
1条回答
  •  被撕碎了的回忆
    2021-01-13 05:21

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

    0 讨论(0)
提交回复
热议问题