我有一个名为ShowAll
的视图,我想在其中调用另一个视图EmployeeRecord
和CustomerRecord
。它给我错误
消息“对象引用未设置为对象的实例。”字符串
在@foreach (var item in Model){
视图中的EmployeeRecord
附近。
我不知道它将如何击中下面的EmployeeRecord
控制器。
任何人都可以帮助我。
ClsShowAll模型
public class ClsShowAll
{
public IEnumerable<ClsEmployeeRecord> clsEmployeeRecord { get; set; }
public IEnumerable<ClsCustomerRecord> clsCustomerRecord { get; set; }
}
控制器
public ActionResult ShowAll()
{
return 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视图
@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>
控制器
public ActionResult EmployeeRecord()
{
return View(Details.EmployeeList());
}
答案 0 :(得分:1)
您需要使用@Html.Partial(...)
。
如果要将模型传递给PartialView
,则可以使用@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>
您可以下载Adam Freeman撰写的Pro ASP.NET MVC 5书的示例源代码,并查看PartialView
的工作原理-