【问题标题】:MVC Display 2 models in 1 viewMVC 显示 1 个视图中的 2 个模型
【发布时间】:2016-02-05 23:26:37
【问题描述】:

控制器

public class CurrentAndHist
{
    public IEnumerable<CurrentApplication> CurrentApplications { get; set; }
    public IEnumerable<History> Histories { get; set; }
}

[Authorize]
public ActionResult Index()
{
    //var ca = db.CurrentApplications.ToList();
    //var hist = db.Histories.ToList();
    //return View(staffs.Where(IsDeleted = false));

    var ca = db.CurrentApplications.ToList();
    var hist = db.Histories.ToList();

    var model = new CurrentAndHist { CurrentApplications = ca, Histories = hist };

    return View(model);
}

查看

@model IEnumerable<LeaveApplication.Models.CurrentApplication>

<body>
    <h3 class="text-custom animated bounceInDown">Current Applications</h3>

    <div class="table-responsive">
        <table class="table animated bounceInDown">
            <tr>
                <th class="col-md-2">
                    @Html.ActionLink("Start Date", "Index")
                </th>
                <th class="col-md-2">
                    @Html.ActionLink("End Date", "Index")
                </th>
                <th class="col-md-1">
                    @Html.ActionLink("Type", "Index")
                </th>
                <th class="col-md-1">
                    @Html.ActionLink("Status", "Index")
                </th>
                <th class="col-md-2">
                    @Html.ActionLink("Application Date", "Index")
                </th>
                <th class="col-md-1">
                    @Html.ActionLink("Period", "Index")
                </th>
            </tr>

        @foreach (var item in Model) {
            <tr>
                <td>
                    <div class="table-text">
                        @Html.DisplayFor(modelItem => item.StartDate)
                    </div>
                </td>
                <td>
                    <div class="table-text">
                        @Html.DisplayFor(modelItem => item.EndDate)
                    </div>
                </td>
                <td>
                    <div class="table-text">
                        @Html.DisplayFor(modelItem => item.LeaveType)
                    </div>
                </td>
                <td>
                    <div class="table-text">
                        @Html.DisplayFor(modelItem => item.AppStatus)
                    </div>
                </td>
                <td>
                    <div class="table-text">
                        @Html.DisplayFor(modelItem => item.UpdateDate)
                    </div>
                </td>
                <td>
                    <div class="table-text">
                        @Html.DisplayFor(modelItem => item.NoOfDays)
                    </div>
                </td>
                <td>
                    <button type="button" class="btn btn-default col-md-3 col-md-offset-1" onclick="location.href='@Url.Action("Edit", new {  })';return false;">
                        <span class="glyphicon glyphicon-pencil"></span>
                    </button>

                    <button type="button" class="btn btn-default col-md-3 col-md-offset-1" onclick="location.href='@Url.Action("Details", new { })';return false;">
                        <span class="glyphicon glyphicon-align-justify"></span>
                    </button>

                    <button type="button" class="btn btn-default col-md-3 col-md-offset-1" onclick="location.href='@Url.Action("Delete", new {  })' ;return false;">
                        <span class="glyphicon glyphicon-trash"></span>
                    </button>
                </td>
            </tr>
        }
        </table>
    </div>

    <br /><br />
    <h3 class="text-custom animated bounceInDown">Application History</h3>

    <div class="table-responsive">
        <table class="table animated bounceInDown">
            <tr>
                <th class="col-md-2">
                    @Html.ActionLink("Start Date", "Index")
                </th>
                <th class="col-md-2">
                    @Html.ActionLink("End Date", "Index")
                </th>
                <th class="col-md-2">
                    @Html.ActionLink("Type", "Index")
                </th>
                <th class="col-md-2">
                    @Html.ActionLink("Status", "Index")
                </th>
                <th class="col-md-2">
                    @Html.ActionLink("Period", "Index")

                </th>
            </tr>

            @foreach (var item in Model) {
            <tr>
                <td>
                    <div class="table-text">
                        @Html.DisplayFor(modelItem => item.StartDate)
                    </div>
                </td>
                <td>
                    <div class="table-text">
                        @Html.DisplayFor(modelItem => item.EndDate)
                    </div>
                </td>
                <td>
                    <div class="table-text">
                        @Html.DisplayFor(modelItem => item.LeaveType)
                    </div>
                </td>
                <td>
                    <div class="table-text">
                        @Html.DisplayFor(modelItem => item.AppStatus)
                    </div>
                </td>
                <td>
                    <div class="table-text">
                        @Html.DisplayFor(modelItem => item.NoOfDays)
                    </div>
                </td>
                <td>
                    <button type="button" class="btn btn-default col-md-3 col-md-offset-1" onclick="location.href='@Url.Action("Details", new { })';return false;">
                        <span style="margin-right: 5px" class="glyphicon glyphicon-align-justify"></span>Details
                    </button>
                </td>
        </table>
    </div>
</body>

我在一个页面中有 2 个表格,两个表格都显示来自不同模型的数据。 Current Applications 表保存来自 CurrentApplications 模型的数据,而 History 表保存来自 Histories 模型的数据。我尝试了其他用户的解决方案 - pass two models to view 但它对我不起作用,我得到了错误

传入字典的模型项是类型 'LeaveApplication.Controllers.LeaveController+CurrentAndHist',但是 这个字典需要一个类型的模型项 'System.Collections.Generic.IEnumerable`1[LeaveApplication.Models.CurrentApplication]'。

我尝试更改为@model IEnumerable&lt;LeaveApplication.Models.CurrentAndHist&gt;,但效果不佳。

【问题讨论】:

    标签: asp.net-mvc view model asp.net-mvc-5 ienumerable


    【解决方案1】:

    将视图中的模型更改为

    @model yourAssembly.CurrentAndHist
    

    并使用 2 个循环来生成项目

    @foreach (var item in Model.CurrentApplications )
    {
      @Html.DisplayFor(m => item.StartDate)
      ....
    }
    @foreach (var item in Model.Histories)
    {
      ....
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-20
      • 2015-10-30
      • 1970-01-01
      • 2016-05-10
      相关资源
      最近更新 更多