簡體   English   中英

MVC在1個視圖中顯示2個模型

[英]MVC Display 2 models in 1 view

調節器

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個表,兩個表都顯示來自不同模型的數據。 當前應用程序表保存來自CurrentApplications模型的數據,而歷史記錄表保存來自歷史記錄模型的數據。 我嘗試了其他用戶的解決方案- 傳遞了兩種模型進行查看,但對我不起作用,並且出現了錯誤

傳遞到字典中的模型項的類型為'LeaveApplication.Controllers.LeaveController + CurrentAndHist',但是此字典需要模型項的類型為'System.Collections.Generic.IEnumerable`1 [LeaveApplication.Models.CurrentApplication]'。

我嘗試更改為@model IEnumerable<LeaveApplication.Models.CurrentAndHist>但效果不佳。

將視圖中的模型更改為

@model yourAssembly.CurrentAndHist

並使用2個循環來生成項目

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM