I have a view that needs to load different component vieww dynamically based on component view name. I am using a function in C# to achieve this:
public ViewComponentResult GetComponentView(string componentName, int id)
{
return ViewComponent(componentName, new { id = id });
}
This is in a controller CT40. So the file structure is:
/Controllers/CT40/CT40Controller
/Views/CT40/Components/Maintenance/Default.cshtml /Views/CT40/Components/Maintenance/MaintenanceViewComponents.cs
Inside MaintenanceViewComponents.cs I have:
[ViewComponent(Name = "Maintenance")]
It works find when I call the GetComponentView function with "Maintenance" as the component name.
But I want to move the GetComponentView function to the HomeController. When I do this, it returns
"System.InvalidOperationException: The view 'Components/Maintenance/Default'"
I have tried every path I can think of, same thing:
/CT04000/Maintenance
/CT04000/Components/Maintenance
/CT04000/Components/Maintenance/Default.cshtml
Views/CT04000/Components/Maintenance/Default.cshtml
Same error every time.
Any idea what path I need to put in to get Homecontroller to look in: /Views/CT40/Components/Maintenance?
I was looking at the problem in the wrong place.
I was trying to put the path to the view in componentName here:
But the path needed to go in statement
in the file MaintenanceViewComponents.cs.
I added the path "~/Views/CT04000/Components/Maintenance/Default.cshtml" and worked liked charm.