繁体   English   中英

如何在一个视图中使用两个 IEnumerable Model?

[英]How to use two IEnumerable Model in one view?

我尝试在一个视图中使用两个模型。 这是我的 model 代码:

Model“公告”

public partial class Announce
{
    public int No { get; set; }
    public string Announce_Subject { get; set; }
    public string Announce_Context { get; set; }
    public string Announce_Author { get; set; }
    public Nullable<System.DateTime> Announce_Date { get; set; }
}

Model 消息

public partial class Message
{
    public int MessageId { get; set; }
    public string MessageTo { get; set; }
    public string MessageFrom { get; set; }
    public string Text { get; set; }
}

Model 共两种型号

public class Total
{
    public IEnumerable<Announce> Announce1 { get; set; }
    public IEnumerable<Message> Message { get; set; }
}

我的观点

@model DemoEmployee.Total

@foreach (var item in Model.Announce1)
{
    <tr>
        <td>
            @Html.ActionLink(item.Announce_Subject, "Details", "Announces", new { id = item.No }, null)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Announce_Author)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Announce_Date)
        </td>
    </tr>
}


@foreach (var item in Model.Message)
{
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.MessageFrom)
        </td>
        <td>
            @Html.ActionLink(item.Text, "Details", new { id = item.MessageId })
        </td>
        <td>
            @Html.ActionLink("Delete", "Delete", new { id = item.MessageId })
        </td>
    </tr>
}

索引控制器

public ActionResult Index()
{
    return View(db.Announce.ToList());
}

我不知道我的问题是什么。 我收到了这个错误:

"The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[DemoEmployee.Announce]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[DemoEmployee.全部的]”

请帮我!

正如您在视图中定义@model DemoEmployee.Total ,您需要在return View()中传递Total的 object 。

将 DemoEmployee.Total 的DemoEmployee.Total传递给View(...)而不是 List of Announce 更新您的return声明如下。

public ActionResult Index()
{
    return View(new DemoEmployee.Total() { Announce1 = db.Announce.ToList(), Message = new List<Message>() });
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM