gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 23:07:38 24 4
gpt4 key购买 nike

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

“索引” View 是员工 Controller 的一部分,“页脚” View 是共享 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”的适用方法,但似乎有一个名为“RenderPartial”的扩展方法。不能动态调度扩展方法。考虑在没有扩展方法语法的情况下转换动态参数或调用扩展方法。

请告诉我在 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com