[英]How to use View Models in MVC to display multiple model in razor view?
[英]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.