Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法强制转换类型为';System.Data.Entity.Infrastructure.DbQuery`1[]和#x27;输入';System.Collections.Generic.IEnumerable_C#_Asp.net Mvc_Casting - Fatal编程技术网

C# 无法强制转换类型为';System.Data.Entity.Infrastructure.DbQuery`1[]和#x27;输入';System.Collections.Generic.IEnumerable

C# 无法强制转换类型为';System.Data.Entity.Infrastructure.DbQuery`1[]和#x27;输入';System.Collections.Generic.IEnumerable,c#,asp.net-mvc,casting,C#,Asp.net Mvc,Casting,我试图通过ViewData将几个ViewModels传递给同一个视图。不幸的是,我是MVC新手,我不知道这有什么问题。 以下是第一个DataViewModel: public class TagsViewModel { public string TagName { get; set; } public int TagId { get; set; } } 还有一个: public class ShortPostViewModel { public int PostId

我试图通过ViewData将几个ViewModels传递给同一个视图。不幸的是,我是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; }
}
因此,我认为:

    <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>
    @foreach(IEnumerable视图数据[“ShortPost”]中的变量项) {
  • @ActionLink(@item.PostSubject,“Details”,“BlogPost”,new{id=item.PostId},null) Сзааааа:@item.PostCreated.Аааа:@item.postgrating。 Темы статьи: @foreach(var标记在@item.PostedTags中) { @ActionLink(@tag.TagName,“tag”,“Search”,new{id=tag.TagId},null) }

  • }
@foreach(IEnumerable视图数据[“标记”]中的变量标记) { @ActionLink(@tag.TagName,“tag”,“Search”,new{id=tag.TagId},null) }

这一切对我来说都很好。您能建议我如何解决这个问题吗?

我建议您使用一个新的ViewModel类,该类具有
列表
属性和
列表
属性,而不是使用多个
视图数据
,这样您就不必在视图中进行转换。假设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; }
}
确保在视图代码的顶部有此项

@model CustomViewModel
您可以在视图中枚举
短贴
的项目,如下所示

@foreach (var item in Model.ShortPosts)
并列举
标记的项目,如下所示

@foreach (var tag in Model.Tags)

哪一行导致了错误?这一行@foreach(在(IEnumerable)ViewData[“ShortPost”]中的变量项),但我想还有另一行。那么
ViewData[“ShortPost”]=“u postRepository.GetLast20().ToList()如何?@DarinDimitrov-无法强制转换“System.Collections.Generic.listThank”类型的对象,让我试试。顺便说一句,据我所知,这种结构现在允许执行CRUD操作,对吗?是的,你是对的,如果你对此有问题,你可以问一个新问题。
public ActionResult Index()
{
    CustomViewModel model = new CustomViewModel();
    model.ShortPosts = _postRepository.GetLast20().ToList();
    model.Tags = _tagsRepository.GetAll().ToList();

    return View(model);
}
@model CustomViewModel
@foreach (var item in Model.ShortPosts)
@foreach (var tag in Model.Tags)