【问题标题】:View not working, Passing object to view error查看不工作,传递对象查看错误
【发布时间】:2014-09-03 05:12:08
【问题描述】:

我正在开发一个在 VS2013 中使用 MVC4 和 EF Data First 的项目。尝试将对象传递给视图时出现此错误。

传入字典的模型项的类型为'System.Data.Entity.Infrastructure.DbQuery1[<>f__AnonymousType34[System.String,System.Nullable1[System.Int32],System.Nullable1[System.Int32],System.Nullable@987654323 @1[TitansMVCWithBootStrap.Models.Player_Game_Stats]'

我不知道如何解决这个问题,任何建议都会有所帮助。谢谢

控制器

 public class PlayerGameStatsController : Controller
{
    private Context db = new Context();

    // GET: PlayerGameStats/Details/5

    public ActionResult Details(int gid=19, int sid = 11)
    {

        var playerGameStats = from pgs in db.PlayerGameStats
                              join p in db.Players on pgs.Player_id equals p.id_player
                              where pgs.SeasonId == 11 && pgs.GameNumber == 19
                              select new
                              {
                                  p.PlayerName,
                                  pgs.PlateAppearance,
                                  pgs.Runs,
                                  pgs.Hits
                              };


        return View(playerGameStats);

    }

型号

 public class Player_Game_Stats
{
    [Key]
    [Column(Order = 0)]
    public int Player_id { get; set; }
    [Key]
    [Column(Order = 1)]
    public int GameNumber { get; set; }
    public Nullable<int> PlateAppearance { get; set; }
    public Nullable<int> Runs { get; set; }
    public Nullable<int> HR { get; set; }
    public int Hits{ get; set; }

    //[ForeignKey("id_player")]
    //public virtual Players Player { get; set; }
}
}

查看

@model IEnumerable<TitansMVCWithBootStrap.Models.Player_Game_Stats>
@{
ViewBag.Title = "Details";
Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Details</h2>

<table class="table table-striped">

<tr>

 <th>
        @Html.DisplayNameFor(model => model.PlayerName)
    </th>
 <th>
        @Html.DisplayNameFor(model => model.PlateAppearance)
    </th>
 <th>
        @Html.DisplayNameFor(model => model.Runs)
    </th>
 <th>
        @Html.DisplayNameFor(model => model.Hits)
    </th>

@foreach (var item in Model)
{

<tr>

<td>
        @Html.DisplayFor(modelItem => item.PlayerName)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.PlateAppearance)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.Runs)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.Hits)
    </td>
</tr>
}
</table>

也试过了

    var playerGameStats = (from pgs in db.PlayerGameStats
                               join p in db.Players on pgs.Player_id equals p.id_player
                               where pgs.SeasonId == 11 && pgs.GameNumber == 19
                               select new Player_Game_Stats
                              {
                                  p.PlayerName,
                                  pgs.PlateAppearance,
                                  pgs.Runs,
                                  pgs.Hits
                              }).ToList();


        return View(playerGameStats);

【问题讨论】:

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


    【解决方案1】:

    试试这个:

    public ActionResult Details(int gid=19, int sid = 11)
    {
    
        var playerGameStats = from pgs in db.PlayerGameStats
                              join p in db.Players on pgs.Player_id equals p.id_player
                              where pgs.SeasonId == 11 && pgs.GameNumber == 19
                              select new TitansMVCWithBootStrap.Models.Player_Game_Stats
                              {
                                  PlayerName = p.PlayerName,
                                  PlateAppearance = pgs.PlateAppearance,
                                  Runs = pgs.Runs,
                                  Hits = pgs.Hits
                              };
    
        return View(playerGameStats);
    
    }
    

    【讨论】:

    • 我收到以下错误。 Cannot initialize type 'TitansMVCWithBootStrap.Models.Player_Game_Stats' with a collection initializer because it does not implement 'System.Collections.IEnumerable' \TitansBootStrap\TitansMVCWithBootStrap\TitansMVCWithBootStrap\Controllers\PlayerGameStatsController.cs TitansMVCWithBootStrap
    • 也试过 'var playerGameStats = (from pgs in db.PlayerGameStats join p in db.Players on pgs.Player_id 等于 p.id_player where pgs.SeasonId == 11 && pgs.GameNumber == 19 选择new Player_Game_Stats { p.PlayerName, pgs.PlateAppearance, pgs.Runs, pgs.Hits }).ToList();返回视图(playerGameStats);`
    • 创建“Player_Game_Stats”类型的新对象时,需要输入“PropertyName” = “Value”,如下所示:select new Player_Game_Stats { PlayerName = p.PlayerName, ... }
    • 试试你的建议,但没有运气。 select new TitansMVCWithBootStrap.Models.Player_Game_Stats { PlayerName = p.PlayerName, PlateAppearance = pgs.PlateAppearance, Runs = pgs.Runs, Hits = pgs.Hits }; TitansMVCWithBootStrap.Models.Player_Game_Stats' 不包含“PlayerName”的定义
    • Players 与 Player_Game_Stats 是一对多的关系。所以一个玩家可以在 Player_Game_Stats 中有多个条目,我使用 where 子句来限制结果。此查询适用于 LinqPad,但不适用于 MVC from pgs in Player_Game_Stats join p in Players on pgs.Player_id equals p.Id_player where pgs.SeasonId == 11 &amp;&amp; pgs.GameNumber == 19 select new { p.PlayerName, pgs.PlateAppearance, pgs.Runs, pgs.Hits }
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-17
    • 2015-02-27
    • 1970-01-01
    相关资源
    最近更新 更多