c# - 添加 ASP.NET MVC5 部分 View 时出现异常

标签 c# asp.net-mvc

我正在尝试将名为“Footer”的部分 View 添加到名为“Index”的 View 中。

“索引” View 是员工 Controller 的一部分,“页脚” View 是共享 View 。

索引查看代码:

@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; }
}

员工 Controller 类:

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”的适用方法,但似乎有一个使用该名称的扩展方法。扩展方法无法动态分派(dispatch)。考虑转换动态参数或在不使用扩展方法语法的情况下调用扩展方法。

请让我知道我在 View 中添加部分 View 时缺少什么。如果我只是删除从“索引” View 添加部分 View “页脚”的调用,该 View 就会正常显示。

最佳答案

更改此行

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

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

关于c# - 添加 ASP.NET MVC5 部分 View 时出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40624277/

相关文章:

c# - 在查询中将 DateTime.Ticks 转换为 MySQL DateTime

c# - 在 protobuf-net v3 更新中替换 IList<>?

C# HttpClient 发布信息不等待响应

c# - 是否可以使用 SQL 查询 XML 文件?

c# - MVC 网站不接收来自客户端的字符串

javascript - 单页应用程序和 ASP.NET MVC

c# .net,加载图像

jquery - Kendo Treeview 在子节点中获得无限循环

asp.net-mvc - 你把既不是 Controller 、模型、助手或 View 模型的类放在哪里?

.net - 将 HTML 渲染为 TIFF