添加ASP异常.. NET MVC5部分视图

本文关键字:视图 MVC5 NET ASP 异常 添加 | 更新日期: 2023-09-27 18:18:19

我想添加一个名为"Footer"的部分视图到一个名为"Index"的视图。

"Index"视图是Employee控制器的一部分,"Footer"视图是一个共享视图。

索引视图代码:

@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',但似乎有一个扩展方法的名称。不能动态分派扩展方法。考虑强制转换动态参数或调用扩展方法而不使用扩展方法语法。

请让我知道我错过了什么在这里添加一个视图的部分视图。如果我只是从"索引"视图中删除一个添加部分视图"Footer"的调用,视图就会出现。

添加ASP异常.. NET MVC5部分视图

改变这一行

@{Html.RenderPartial("Footer", Model.FooterData);}

@Html.Partial("Footer", (FooterViewModel)Model.FooterData)