简体   繁体   English

在Telerik MVC网格中显示实体框架模型

[英]Showing Entity Framework model in Telerik MVC Grid

Currently I have this AcceptedProposals view, which is meant to show the details of a list of proposals, which are coming from the Entity Framework. 当前,我具有这个AcceptedProposals视图,该视图用于显示来自实体框架的建议列表的详细信息。 Here's the code I have in my controller: 这是我控制器中的代码:

public ActionResult AcceptedProposals()
{
    var proposals = db.Proposals.Where(p => p.CollegeFundDecision == true);
    return View(proposals);
}

In my view I have the following line, but it's giving me this error: 在我看来,我有以下内容,但它给了我这个错误:

System.InvalidOperationException: The model item passed into the dictionary is of type 'System.Data.Objects.ObjectQuery`1[URGLibrary.Proposal]', but this dictionary requires a model item of type 'URGLibrary.Proposal'. System.InvalidOperationException:传递到字典中的模型项的类型为'System.Data.Objects.ObjectQuery`1 [URGLibrary.Proposal]',但此字典需要类型为'URGLibrary.Proposal'的模型项。

@(Html.Telerik().Grid<Proposal>((IEnumerable<Proposal>)Model))

Any idea how I would be able to this this grid to show up properly? 知道如何将这个网格正确显示吗? And further more, once I get this working, if I wanted to make it so that there's a dropdown of years to choose what year of proposals I'm looking at, would I be putting this grid code into a partial view? 而且,一旦我开始工作,如果我想这样做,那么可以花几年的时间来选择我要看的提案的年份,我是否会将此网格代码放到局部视图中?

Thanks for the help! 谢谢您的帮助!

You havent enumerated your set prior to passing it to the view. 在将其传递给视图之前,您没有枚举您的集合。 Its good practice to use .ToArray or .ToList to pre-enumerate your set in the controller so that you cant accidentally add bits to it in the view. 使用.ToArray或.ToList在控制器中预先枚举您的设置是一个好习惯,这样您就不会在视图中意外地向其中添加位。 This means your action should look like this: 这意味着您的操作应如下所示:

public ActionResult AcceptedProposals()
{
    var proposals = db.Proposals.Where(p => p.CollegeFundDecision == true).ToArray();
    return View(proposals);
}

This relates to the ObjectQuery part of the error (a collection which hasnt yet been finalised and retrieved from the database yet). 这与错误的ObjectQuery部分有关(一个集合尚未完成并从数据库中检索出来)。

Try this and let me know if it resolves the issue or gives you a simpler error message. 尝试执行此操作,让我知道它是否可以解决问题或为您提供更简单的错误消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM