【问题标题】:2 models inside an IEnumerableIEnumerable 中的 2 个模型
【发布时间】:2014-03-29 22:37:27
【问题描述】:

我在尝试正确获取模型时遇到了困难,无论如何我可以在 IEnumerable 中安装 2 个模型吗?这就是我所拥有的..

public class threadreplying
{

    public IEnumerable<profile,Threadpost> IEThreadpost { get; set; }
    public taker orginalthread { get; set; }


}

IEnumerable 只需要 1 个参数我怎样才能使 IEthreadpost 工作?我这样做是因为我在我的 sql 中实现了一个 Join 并需要来自存储在数据库中的两个模型的信息。我也试过这样做

 public class ModelMix
{
    public profile profiles { get; set; }
    public Threadpost threadposts { get; set; }
}

public class threadreplying
{

    public IEnumerable<ModelMix> IEThreadpost { get; set; }
    public taker orginalthread { get; set; }


}

这可行,但它在视图中不起作用,即使我正在检查它也会给我一个空错误

@if (Model.IEThreadpost != null)
{
    foreach (var item in Model.IEThreadpost)
    {
        @item.threadposts.post
    }
   }

【问题讨论】:

  • 您收到错误是因为 threadposts 属性为空。

标签: asp.net-mvc-3 ienumerable


【解决方案1】:

您需要检查@item.threadposts 是否为空。

@if (Model.IEThreadpost != null)
{
    foreach (var item in Model.IEThreadpost)
    {
        @if (item.threadposts != null)
        {
            @item.threadposts.post
        }
    }
}

当然,您应该提前认识到进行该检查的含义是什么,并且您应该确保这不是您之前的逻辑的指示,您实际上是在哪里执行select

【讨论】:

    猜你喜欢
    • 2015-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-28
    • 1970-01-01
    • 2012-05-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多