传递到字典中的模型项的类型为'System.Data.Objects.ObjectResult`1

时间:2011-12-17 20:58:55

标签: asp.net-mvc asp.net-mvc-3 entity-framework stored-procedures entity-framework-4

我正在尝试传递映射到名为ListLeagueLeaders()的方法的存储过程,但是自动生成的方法用于调用存储过程,ObjectResult<ListLeagueLeaders_Result> ListLeagueLeaders()期望一个ObjectResult类型。我该怎么做才能解决这个问题?

我想过使用LINQ而不是存储过程,但我想给这个策略一个最后的机会。相关:LINQ to Entities instead of stored procedure?

的HomeController

    public class HomeController : Controller
    {

        HockeyStatsEntities db = new HockeyStatsEntities();

        public ActionResult Index()
        {
            ViewBag.Message = "League leaders";
            {
                return View(db.ListLeagueLeaders());
            }
        }

        private ICollection<ListLeagueLeaders_Result> ListLeagueLeaders()
        {
            ICollection<ListLeagueLeaders_Result> leagueLeadersCollection = null;

            using (HockeyStatsEntities context = new HockeyStatsEntities())
            {
                foreach (ListLeagueLeaders_Result leagueLeader in
                          context.ListLeagueLeaders())
                {
                    leagueLeadersCollection.Add(leagueLeader);
                }
            }
            return leagueLeadersCollection;
        }
    }

Model1.Context.cs

public partial class HockeyStatsEntities : DbContext
    {

        public DbSet<Dim_Date> Dim_Date { get; set; }
        public DbSet<Dim_Player> Dim_Player { get; set; }
        public DbSet<Dim_Team> Dim_Team { get; set; }
        public DbSet<Fact_Statistics> Fact_Statistics { get; set; }
        public DbSet<Dim_Game> Dim_Game { get; set; }

        public virtual ObjectResult<ListLeagueLeaders_Result> ListLeagueLeaders()
        {
            ((IObjectContextAdapter)this).ObjectContext.MetadataWorkspace.LoadFromAssembly(typeof(ListLeagueLeaders_Result).Assembly);

            return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<ListLeagueLeaders_Result>("ListLeagueLeaders");
        }
    }

查看:

@model NHLStats2.Models.ListLeagueLeaders_Result

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

1 个答案:

答案 0 :(得分:1)

您的View上出现此错误。您需要引用从控制器操作重新调整的相同对象类型。

例如,如果您从控制器操作中返回List<Foo>类型,则只能将视图强力输入List<Foo>IList<Foo>IEnumerable<Foo>