无法转换类型为#System; System.Data.Entity.Infrastructure.DbQuery`1 []'的对象键入' System.Collections.Generic.IEnumerable

时间:2014-11-16 11:25:37

标签: c# asp.net-mvc casting

我正在尝试通过ViewData将几个ViewModel传递给同一个View。不幸的是,我是MVC的新手,我不知道这有什么问题。 这是第一个DataViewModel:

public class TagsViewModel
{
    public string TagName { get; set; }
    public int TagId { get; set; }
} 

还有一个:

public class ShortPostViewModel
{
    public int PostId { get; set; }
    public string PostSubject { get; set; }
    public DateTime? PostCreated { get; set; }
    public string PostImage { get; set; }
    public string PostAuthor { get; set; }
    public byte? PostRating { get; set; }
    public List<PostTagsViewModel> PostedTags { get; set; }
}

这是存储库:

public IEnumerable<BlogPostViewModel.ShortPostViewModel> GetLast20()
{
    var last = from a in _db.blog_post
               orderby a.Posted descending
               select new BlogPostViewModel.ShortPostViewModel
               {
                   PostId = a.ID,
                   PostAuthor = (from u in _db.users where u.ID == a.Author 
                                 select u.Login).FirstOrDefault(),
                   PostCreated = a.Posted,
                   PostImage = a.PostAvatar,
                   PostRating = a.Rating,
                   PostSubject = a.Subject,
                   PostedTags = (from b in _db.tags
                                join c in _db.posted_tags on b.ID equals c.TagID
                                where c.PostID == a.ID
                                select new PostTagsViewModel 
                                { 
                                    TagId = b.ID, 
                                    TagName = b.TagName
                                }).ToList()
               };
    return last.Take(20);
}

还有一个:

public IEnumerable<TagsViewModel> GetAll()
{
    var t = from a in _db.tags
        select new TagsViewModel
        {
            TagId = a.ID,
            TagName = a.TagName
        };
    return t;
}

所以这是控制器:

public ActionResult Index()
{
    ViewData["ShortPost"] = _postRepository.GetLast20().AsEnumerable();
    ViewData["Tags"] = _tagsRepository.GetAll().AsEnumerable();
    return View();
}

所以在视图上:

    <ul class="list-group">
        @foreach (var item in (IEnumerable<ShortPostViewModel>)ViewData["ShortPost"])
        {
            <li class="list-group-item">
                <img src="@item.PostImage" alt=""/>
                <h3>@Html.ActionLink(@item.PostSubject, "Details", "BlogPost", new { id = item.PostId }, null)</h3>
                Создано: @item.PostCreated. Автор: @item.PostAuthor. Оценка: @item.PostRating.
                <p>
                    Темы статьи:
                    @foreach (var tag in @item.PostedTags)
                    {
                        <i class="glyphicon glyphicon-tag"></i> @Html.ActionLink(@tag.TagName, "Tag", "Search", new { id = tag.TagId }, null)
                    }
                </p>
            </li>
        }
    </ul>
</div>
<div class="col-md-4">
    @foreach (var tag in (IEnumerable<TagsViewModel>)ViewData["Tags"])
    {
        <span class="label label-info"><i class="glyphicon glyphicon-tag"></i> @Html.ActionLink(@tag.TagName, "Tag", "Search", new { id = tag.TagId }, null)</span>
    }
</div>

这一切对我来说都很好。你能告诉我该如何解决这个问题?

1 个答案:

答案 0 :(得分:2)

我建议使用具有ViewData属性和List<TagsViewModel>属性的新ViewModel类,而不是使用多个List<ShortPostViewModel>,这样您就不必在视图。假设ViewModel名为CustomViewModel

public class CustomViewModel
{
    public CustomViewModel()
    {
        this.ShortPosts = new List<ShortPostViewModel>();
        this.Tags = new List<TagsViewModel>();
    }

    public List<ShortPostViewModel> ShortPosts { get; set; }
    public List<TagsViewModel> Tags { get; set; }
}

然后在您的控制器中

public ActionResult Index()
{
    CustomViewModel model = new CustomViewModel();
    model.ShortPosts = _postRepository.GetLast20().ToList();
    model.Tags = _tagsRepository.GetAll().ToList();

    return View(model);
}

确保将此视图放在视图代码的顶部

@model CustomViewModel

您可以在视图中枚举ShortPosts的项目,如下所示

@foreach (var item in Model.ShortPosts)

并枚举Tags项目如下

@foreach (var tag in Model.Tags)