Manju Manju - 1 year ago 86
C# Question

Exception on adding ASP.NET MVC5 Partial Views

I'm trying to add a partial view named "Footer" to a view named "Index".

"Index" view is part of Employee controller and "Footer" view is a shared view.

Index View code:

@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**

Footer View code:

@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

EmployeeListViewModel class:

public class EmployeeListViewModel
public List<EmployeeViewModel> employees { get; set; }
public string UserName { get; set; }
public FooterViewModel FooterData { get; set; }

FooterViewModel class:

public class FooterViewModel
public string CompanyName { get; set; }
public string Year { get; set; }

Employee controller class:

public class EmployeeController : Controller
// GET: Test
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");
empListViewObj.employees = lstEmpView;
empListViewObj.FooterData = new FooterViewModel();
empListViewObj.FooterData.CompanyName = "Company name";
empListViewObj.FooterData.Year = DateTime.Now.Year.ToString();
return View("Index", empListViewObj);

Exception message:
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS1973: 'System.Web.Mvc.HtmlHelper' has no applicable method named 'RenderPartial' but appears to have an extension method by that name. Extension methods cannot be dynamically dispatched. Consider casting the dynamic arguments or calling the extension method without the extension method syntax.

Please let me know what am i missing here to add a partial view in a view. If i just remove a call to add a partial view "Footer" from "Index" view, The view comes up just fine.

Answer Source

Change this line

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


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