我正在尝试将名为“Footer”的部分视图添加到名为“Index”的视图中。
“索引”视图是员工控制器的一部分,“页脚”视图是共享视图。
索引查看代码:
@using MVC_Demo1.Models
@Model EmployeeListViewModel
@{
Layout = null;
}
<h2>Hello, @Model.UserName</h2>
<a href="/Authentication/Logout">Logout</a>
<a href="/Employee/AddNew">Add New</a>
@foreach (EmployeeViewModel empObj in Model.employees)
{
<div>Name: @empObj.EmployeeName</div>
<div>Salary: @empObj.EmployeeSalary</div>
<div>Role: @empObj.EmployeeRole</div>
<br />
}
@{Html.RenderPartial("Footer", Model.FooterData);} **// Getting error in this line on trying to add a partial view Footer**
页脚查看代码:
@using MVC_Demo1.ViewModel
@Model FooterViewModel
<div style="text-align:right;background:silver;color:darkcyan;border:1px solid gray; margin-top:2px; padding-right: 10px;">
@Model.CompanyName @Model.Year
</div>
EmployeeListViewModel类:
public class EmployeeListViewModel
{
public List<EmployeeViewModel> employees { get; set; }
public string UserName { get; set; }
public FooterViewModel FooterData { get; set; }
}
FooterViewModel类:
public class FooterViewModel
{
public string CompanyName { get; set; }
public string Year { get; set; }
}
员工控制器类:
public class EmployeeController : Controller
{
// GET: Test
[Authorize]
public ActionResult Index()
{
EmployeeBusinessLayer empBizObj = new EmployeeBusinessLayer();
List<Employee> lstEmps = new List<Employee>();
lstEmps = empBizObj.GetEmployees();
EmployeeListViewModel empListViewObj = new EmployeeListViewModel();
empListViewObj.UserName = User.Identity.Name;
List<EmployeeViewModel> lstEmpView = new List<EmployeeViewModel>();
foreach (Employee emp in lstEmps)
{
EmployeeViewModel empViewObj = new EmployeeViewModel();
empViewObj.EmployeeName = emp.FName + " " + emp.LName;
empViewObj.EmployeeSalary = emp.Salary.ToString("c");
empViewObj.SetEmployeeRole(emp.Salary);
lstEmpView.Add(empViewObj);
}
empListViewObj.employees = lstEmpView;
empListViewObj.FooterData = new FooterViewModel();
empListViewObj.FooterData.CompanyName = "Company name";
empListViewObj.FooterData.Year = DateTime.Now.Year.ToString();
return View("Index", empListViewObj);
}
}
异常消息: 描述:编译服务此请求所需的资源时发生错误。请查看以下具体错误详细信息并相应地修改源代码。
编译器错误消息:CS1973:'System.Web.Mvc.HtmlHelper'没有名为'RenderPartial'的适用方法,但似乎有一个名称的扩展方法。无法动态分派扩展方法。考虑转换动态参数或调用扩展方法而不使用扩展方法语法。
请让我知道我在这里缺少的是在视图中添加局部视图。如果我只是删除了一个从“索引”视图添加部分视图“页脚”的调用,那么该视图就可以了。
答案 0 :(得分:2)
更改此行
@{Html.RenderPartial("Footer", Model.FooterData);}
要
@Html.Partial("Footer", (FooterViewModel)Model.FooterData)