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

问题描述 投票:0回答:2

我希望假期对所有人都有好处。

我正在使用MVC并使用ADO.net实体框架连接到我的数据库,因此它为我创建了我的实体。

我正在尝试创建一个将使用两个模型的视图 - Matchups和Players。每个输出列表的存储过程列表,我将放在页面上的两个选项卡下。我发现了一篇文章,解释了将两个模型绑定到一个视图的几种不同方法。经过一番努力,我已经确定了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>

那么为了让类型变得相同,我应该改变什么呢?对不起,我对这种语法不够好,无法找到它的确切位置。

谢谢大家的帮助!

c# asp.net-mvc entity-framework asp.net-mvc-4
2个回答
2
投票

由于某种原因,您的MatchupList返回包含在视图中的列表:

return View(db.usp_GetMatchups())

只有在将其用作MVC操作时才需要。似乎并非如此 - 您似乎将其用作实际操作的实用方法。

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

FTBL.Matchup = db.usp_GetMatchups();

目前还不是很清楚ObjectResult是什么,但假设这是某种列表,这应该可以帮助你摆脱异常。


1
投票

我建议创建一个模型,其中包含您要在视图中呈现的内容,而不是使用dynamic,以便您的视图将被强类型化。然后你就会有智慧,剩下的就容易多了。

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,但它们不经常出现。任何可以强类型的东西都应该是。这样你就可以在编译器中得到错误。否则你必须运行应用程序,如果你做了一些非常小的事情,如拼写错误的属性或引用错误的模型,你将得到一个运行时错误。

© www.soinside.com 2019 - 2024. All rights reserved.