MVC在1个视图中显示2个模型

时间:2015-11-05 08:00:39

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

控制器

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>

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

  

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

我尝试更改为@model IEnumerable<LeaveApplication.Models.CurrentAndHist>,但效果不佳。

1 个答案:

答案 0 :(得分:4)

将视图中的模型更改为

@model yourAssembly.CurrentAndHist

并使用2个循环生成项目

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