I am trying to display a ViewComponent passing it a string parameter, but cannot get this to work. Instead of looking for MyViewComponent\Default.cshtml, if, for instance, I was to pass "FooBar" as the value of the parameter, it is looking for MyViewComponent\FooBar.cshtml. I assume Umbraco overrides the routing . The Umbraco documentation doesn't contain any reference to passing parameters to a ViewComponent. Would anyone be able to help me out with this?
Actually I responded too early to your answer, and realized I still have the same problem. I assume the Template is the name of the ViewComponent .cshtml file, in my case Default. But if I call
actually string Template is something I pass so that I can reuse the same viewcomponent class for different templates, it is not something passed by default and is not required for viewcomponents to work. It seems to me you are not creating yours correctly,
If it is not finding your viewcomponent then it is possibly not named correctly
your views should be in
Using a ViewComponent with a Parameter
I am trying to display a ViewComponent passing it a string parameter, but cannot get this to work. Instead of looking for MyViewComponent\Default.cshtml, if, for instance, I was to pass "FooBar" as the value of the parameter, it is looking for MyViewComponent\FooBar.cshtml. I assume Umbraco overrides the routing . The Umbraco documentation doesn't contain any reference to passing parameters to a ViewComponent. Would anyone be able to help me out with this?
Hi Graeme,
This is what I do.
method signature in ViewComponent class
Calling ViewComponent in a view
Hi Huw,
Actually I responded too early to your answer, and realized I still have the same problem. I assume the Template is the name of the ViewComponent .cshtml file, in my case Default. But if I call
I still get an error saying that
followed by a list of the standard ViewComponent directories but looking for FooBar.cshtml in each case.
actually string Template is something I pass so that I can reuse the same viewcomponent class for different templates, it is not something passed by default and is not required for viewcomponents to work. It seems to me you are not creating yours correctly,
If it is not finding your viewcomponent then it is possibly not named correctly your views should be in
Views\shared\components\YOURVIEWCOMPONENTNAME\Default.cshtml
I use the string Template so that I can do the following in my viewcomponent, and return whichever template I require
What does the code for your viewcomponent look like?
is working on a reply...