Reputation:
Controller
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);
}
View
@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>
I have 2 tables in a page and both tables display data from different models. Current Applications table holds data from the CurrentApplications model while History table holds data from the Histories model. I tried solutions from other users - pass two models to view but it didn't work for me and I got the error
The model item passed into the dictionary is of type 'LeaveApplication.Controllers.LeaveController+CurrentAndHist', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[LeaveApplication.Models.CurrentApplication]'.
I tried changing to @model IEnumerable<LeaveApplication.Models.CurrentAndHist>
but it doesn't work as well.
Upvotes: 0
Views: 209
Reputation:
Change the model in your view to
@model yourAssembly.CurrentAndHist
and use 2 loops to generate the items
@foreach (var item in Model.CurrentApplications )
{
@Html.DisplayFor(m => item.StartDate)
....
}
@foreach (var item in Model.Histories)
{
....
}
Upvotes: 4