MVC - 两个模型绑定到同一视图的转换问题

MVC - conversion issue with two models binding to the same view

希望假期对大家来说都是美好的。

我正在使用 MVC 并使用 ADO.net entity framework 连接到我的数据库,所以它为我创建了我的实体。

我正在尝试创建一个将使用两个模型的视图 - 比赛和球员。每个输出列表的存储过程我将放在页面上的两个选项卡下。我发现一篇文章解释了将两个模型绑定到一个视图的几种不同方法。经过一些努力,我选择了 ExpandoObject()。但是,当我打开我的页面时出现以下错误:

Cannot implicitly convert type 'System.Web.Mvc.ViewResult' to 
'System.Collections.IEnumerable'. An explicit conversion exists (are you 
missing a cast?)

这是我的模型上下文:

public virtual ObjectResult<usp_GetMatchups_Result> usp_GetMatchups()
        {
            return 
 ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<usp_GetMatchups_Result>("usp_GetMatchups");
        }

        public virtual ObjectResult<usp_GetAllNFLPlayers_Result> usp_GetAllPlayers()
        {
            return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<usp_GetAllPlayers_Result>("usp_GetAllPlayers");
        }

我的控制器:

namespace SchmuckSports.Controllers
{
    public class MatchupController : Controller
    {
        TrinoviEntities db = new TrinoviEntities();

        public ActionResult MatchupList()
        {
            return View(db.usp_GetMatchups());
        }

        public ActionResult PLayerList()
        {
            return View(db.usp_GetAllPlayers());
        }

        public ActionResult FtblDashboard()
        {
            dynamic FTBL = new ExpandoObject();
            FTBL.Matchup = MatchupList() ;
            FTBL.Players = PLayerList();
            return View(FTBL);
        }
    }
}

我的观点是这样的(为简单起见省略了一堆,但如果您需要更多,请告诉我):

@model dynamic
@using SchmuckSports.Controllers;


@{
    ViewBag.Title = "Football Den";
}

<tbody>
      @foreach (var match in Model.Matchup)
          {
             <tr onclick="location.href= '@Url.Action("FtblDashboard", "Matchup", new { GameID = match.GameID })'">
                  <td>
                       @match.Date
                  </td>
                  <td>
                       @match.TeamAbbrev.Replace("&#x0D;", "<br />")
                  </td>
                  <td>
                       @match.Spread.Replace("&#x0D;", "<br />")
                  </td>
                  <td>
                       @match.Total.Replace("&#x0D;", "<br />")
                  </td>
                  </tr>
</tbody>

那么我应该更改什么才能使类型相同?抱歉,我对这种语法还不够好,无法找到它到底出了什么问题。

感谢大家的协助!

您的 MatchupList returns 出于某种原因包装到视图中的列表:

return View(db.usp_GetMatchups())

仅当您将其用作 MVC 操作时才需要。情况似乎并非如此 - 您似乎将其用作实际操作的实用方法。

鉴于 MatchupList 方法的简单性,我会完全跳过它:

FTBL.Matchup = db.usp_GetMatchups();

不太清楚 ObjectResult 是什么,但假设这是某种列表,这应该可以帮助您摆脱异常。

我建议创建一个包含您要在视图中呈现的内容的模型,而不是使用 dynamic,这样您的视图将是强类型的。然后你就有了intellisene,剩下的就简单多了。

public class DashboardViewModel
{
    public MatchupList Matchups {get;set;
    public PlayerList Players {get;set;}
}

public ActionResult FtblDashboard()
{
    var matchups = db.usp_GetMatchups();
    var players = db.usp_GetAllPlayers()
    var viewModel = new DashboardViewModel
        { Matchups = matchups, Players = players };        
    return View(viewModel);
}

然后从

开始你的观点
@model DashboardViewModel

既然一切都是强类型的,剩下的就容易多了。如果您在不同的选项卡上显示数据,您可能还会发现将每个选项卡创建为单独的局部视图会更容易。这将使每个视图更小且更易于管理。

使用 dynamic 是有正当理由的,但并不经常出现。任何可以强类型化的东西都应该是。这样你就可以在编译器中得到你的错误。否则你必须 运行 应用程序,如果你做了一些非常小的事情,比如拼错 属性 或引用错误的模型,你将收到 运行 时间错误。