2

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
</div>

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
    [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);
    }
}

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.

2
  • Remove the curly braces around html.renderpartial and end of line (;) Commented Nov 16, 2016 at 4:59
  • @garethb Tried to update the code as @Html.RenderPartial("Footer", @Model.FooterData); and @Html.RenderPartial("Footer", Model.FooterData); But still ending up in the same error Commented Nov 16, 2016 at 5:03

1 Answer 1

4

Change this line

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

To

@Html.Partial("Footer", (FooterViewModel)Model.FooterData)
Sign up to request clarification or add additional context in comments.

2 Comments

Still the same error, Now instead of 'RenderPartial', It says 'Partial' in the exception message.
See update. You need to cast the value you are passing to RederPartial. It will default to type dynamic as the Model on your index page is a different type to FooterData (different namespace as well)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.