无法将类型为“System.Data.Entity.Infrastructure.DbQuery”1[]“的对象强制转换
本文关键字:对象 转换 Infrastructure 类型 System Entity Data DbQuery | 更新日期: 2023-09-27 18:30:35
我正在尝试通过ViewData将几个ViewModels传递给同一个视图。不幸的是,我是MVC的新手,我不知道这有什么问题。这是第一个数据视图模型:
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>
这一切对我来说看起来都很好。你能告诉我我应该如何解决这个问题吗?
与其使用多个ViewData
,我建议使用具有List<TagsViewModel>
属性和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)