加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – 如何在一个视图中使用两个IENumerable模型

发布时间:2020-12-16 04:27:52 所属栏目:asp.Net 来源:网络整理
导读:我试图在一个视图中使用两个模型,但据我所知,我的程序只是在模型中看不到任何对象. 这是我的代码. 楷模: public class Album { [Key] public int ThreadId { get; set; } public int GenreId { get; set; } public string Title { get; set; } public strin
我试图在一个视图中使用两个模型,但据我所知,我的程序只是在模型中看不到任何对象.

这是我的代码.

楷模:

public class Album
    {
        [Key]
        public int ThreadId { get; set; } 
        public int GenreId { get; set; }
        public string Title { get; set; }
        public string ThreadByUser { get; set; } 
        public string ThreadCreationDate { get; set; }
        public string ThreadContent { get; set; } 
        public Genre Genre { get; set; }
        public List<Posts> Posty { get; set; }
    }
    public class Posts 
    {
        [Key]
        public int PostId { get; set; }
        public int ThreadId { get; set; } 
        public string PostTitle { get; set; }
        public string PostContent { get; set; }
        public string PostDate { get; set; }
        public string PosterName { get; set; }
        public Album Album { get; set; }

    }

    public class ModelMix
    {
        public IEnumerable<Posts> PostsObject { get; set; }
        public IEnumerable<Album> ThreadsObject { get; set; }
    }

索引控制器代码:

public ActionResult Index(int id)
    {

        ViewBag.ThreadId = id;
        var posts = db.Posts.Include(p => p.Album).ToList();
        var albums = db.Albums.Include(a => a.Genre).ToList();

        var mixmodel = new ModelMix
        {
            PostsObject = posts,ThreadsObject = albums
        };

        return View(mixmodel);
    }

查看代码:

@model MvcMusicStore.Models.ModelMix

<h2>Index</h2>

@Html.DisplayNameFor(model => model.PostsObject.PostContent)

当我尝试执行我的程序时,我收到此错误:

CS1061: The ”
System.Collections.Generic.IEnumerable ‘does not contain a definition
of” PostContent “not found method of expanding” PostContent “,which
takes a first argument of type’ System.Collections.Generic.IEnumerable

我怎样才能让它按预期工作?在互联网上有很多像我这样的问题,但我找不到任何匹配我的情况.

解决方法

在MVC中循环模型可能有点令人困惑,只是因为模板化帮助程序(即Html.DisplayFor和Html.EditorFor)可以提供模板,帮助程序将自动为集合中的每个元素调用这些模板.这意味着如果你是MVC的新手,并且你没有意识到DisplayTemplate或者还没有为该集合提供EditorTemplate,那么它看起来就像一个简单的:
@Html.DisplayFor(m => m.SomePropertyThatHoldsACollection)

是你所需要的全部.因此,如果您已经看过类似的东西,这可能就是您做出假设的原因.但是,我们暂时假设还没有提供模板.你有两个选择.

首先,最简单的说,就是在集合中使用foreach:

@foreach (var post in Model.PostsObject)
{
    @Html.DisplayFor(m => post.PostTitle)
    // display other properties
}

你也可以使用for循环,但是使用IEnumerable< T>,没有索引器,所以这不起作用:

@for (int i = 0; i < Model.PostsObject.Count(); i++)
{
    // This generates a compile-time error because
    // the index post[i] does not exist.
    // This syntax would work for a List<T> though.
    @Html.DisplayFor(m => post[i].PostTitle)
    // display other properties
}

如果你确实想要使用for循环,你可以像这样使用它:

@for (int i = 0; i < Model.PostsObject.Count(); i++)
{
    // This works correctly
    @Html.DisplayFor(m => post.ElementAt(i).PostTitle)
    // display other properties
}

因此,请使用您喜欢的任何一种.但是,在某些时候,考虑提供templates for your types是个好主意.(注意:尽管本文是为MVC 2编写的,但建议仍然适用.)它们允许您从视图中删除循环逻辑,使它们更清晰.当与Html.DisplayFor或Html.EditorFor结合使用时,它们还将为模型绑定生成正确的元素命名(这很棒).它们还允许您重用类型的演示文稿.

我要做的最后一个评论是你的属性的命名有点冗长:

public class ModelMix
{
    public IEnumerable<Posts> PostsObject { get; set; }
    public IEnumerable<Album> ThreadsObject { get; set; }
}

我们已经知道它们是对象,所以最后不需要添加它.这更具可读性:

public class ModelMix
{
    public IEnumerable<Posts> Posts { get; set; }
    public IEnumerable<Album> Threads { get; set; }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读