asp.net-mvc - 如何在一个 View 中使用两个 IEnumerable 模型

标签 asp.net-mvc asp.net-mvc-4 model ienumerable

我试图在一个 View 中使用两个模型,但据我了解,我的程序在模型中看不到任何对象。

这是我的代码。

型号:

    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; }
    }  

索引 Controller 代码:

    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.DisplayForHtml.EditorFor )提供模板,助手将自动为集合中的每个元素调用这些模板。这意味着如果您是 MVC 新手,并且您没有意识到 DisplayTemplateEditorTemplate尚未为该集合提供,它看起来很简单:

@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 编写的,但建议仍然适用。)它们允许您从 View 中删除循环逻辑,使它们保持干净。与 Html.DisplayFor 结合使用时,或Html.EditorFor ,它们还将为模型绑定(bind)生成正确的元素命名(这很棒)。它们还允许您重复使用某种类型的表示。

我最后要说的一点是,您的属性命名有点冗长:

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; }
}

关于asp.net-mvc - 如何在一个 View 中使用两个 IEnumerable 模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20953877/

相关文章:

asp.net-mvc - ASP.Net MVC Html.HiddenFor 的值错误

c# - 如何在 .net 中有条件地继承类

php - Codeigniter 连接多个 id 并显示到 View

c++ - TreeView QML 错误 - QVariant::QVariant(void*)' 在此上下文中是私有(private)的

asp.net - 获取 ASP.NET MVC 中 Content 文件夹中文件的所有路径

c# - 添加 taghelper 后出现错误 500

asp.net-mvc - HTML 标记破坏 Razor 使用

html - 为外部登录列表按钮提供图像

c# - 解析 "Singleton"内的 InstancePerHttpRequest

database - 如何用 Go 制作模型