Reputation: 33
I have a View called ShowAll
where I want to call another View EmployeeRecord
and CustomerRecord
. It gives me error
Message "Object reference not set to an instance of an object." string
near @foreach (var item in Model){
in EmployeeRecord
View.
I do not how it will hit the below EmployeeRecord
Controller.
Can anyone please help me.
ClsShowAll model
public class ClsShowAll
{
public IEnumerable<ClsEmployeeRecord> clsEmployeeRecord { get; set; }
public IEnumerable<ClsCustomerRecord> clsCustomerRecord { get; set; }
}
Controller
public ActionResult ShowAll()
{
return View();
}
View "ShowAll"
@model xx.xx.xx.ShowAll.ClsShowAll
<div class="container">
<div class="panel panel-default">
<div class="panel-body">
@RenderPage("~/Views/EmployeeRecord/EmployeeRecord.cshtml")
</div>
</div>
<div class="panel panel-default">
<div class="panel-body">
@RenderPage("~/Views/CustomerRecord/CustomerRecord.cshtml")
</div>
</div>
</div>
EmployeeRecord View
@model IEnumerable<xx.xx.xx.EmployeeRecord.ClsEmployeeRecord>
<table>
<tr>
<th>
@Html.DisplayNameFor(m => m.EmployeeName)
</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.EmployeeName)
</td>
</tr>
}
</table>
Controller
public ActionResult EmployeeRecord()
{
return View(Details.EmployeeList());
}
Upvotes: 0
Views: 85
Reputation: 62300
You need to use @Html.Partial(...)
.
If you want to pass a model to PartialView
, then you can use @Html.Partial("EmployeeRecord", model.Employee)
<div class="container">
<div class="panel panel-default">
<div class="panel-body">
@Html.Partial("EmployeeRecord")
</div>
</div>
<div class="panel panel-default">
<div class="panel-body">
@Html.Partial("CustomerRecord")
</div>
</div>
</div>
You can download the sample source code of Pro ASP.NET MVC 5 book by Adam Freeman, and see how PartialView
work in -
Upvotes: 1