I have made a cshtml file in App_Code with global helper methods.
The problem is that i get an exception after build.
Compilation Error
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: CS0534: 'helper' does not implement inherited abstract member 'WebPageExecutingBase.Execute()'
Source Error:
Line 50: public class helper : UmbracoViewPage {
The line var type = i.GetType(); gets information about a type, but not a type. In c# types are static, which means you need have it available at compile time.
IEnumerable<IPublishedContent> or IEnumerable<IPublishedElement> ??
It's hard to tell, but it's likely your img, implements IPublishedContent so you could probably use that as your type in your razor helper.
@helper RenderImage(IPublishedContent i)
??
If your razor helper, is rendering HTML would it be better to have another PartialView for rendering images, that you called like Html.Partial("RenderImagePartial", model);
Global razor helper umb 8
Hi,
I have made a cshtml file in App_Code with global helper methods.
The problem is that i get an exception after build.
My code look like this
Can anyone help?
Hi Peter
I'm not sure you need to inherit UmbracoViewPage at the top?
essentially you are saying that your @helper class inherits from UmbracoViewPage and this requires you to implement
WebPageExecutingBase.Execute()
Which you 'could' do, but I think if you remove the @inherits statement then you won't need to and your helper will still work...
regards
Marc
Hi,
Thanks that helped.
I have another problem after this.
In the global helper, how do i convert the dynamic object thats passed into the method into the correct type?
I tried something like this, but thats not correct
Hi,
The line
var type = i.GetType();
gets information about a type, but not a type. In c# types are static, which means you need have it available at compile time.Your
RenderImage
could be rewritten as:Another rewrite could be:
IContentWithImage would be a composition "Content With Image" that defines the
Image
-propertyI know this is a very simple example, i have other more complex stuff i would like to have in a global helper file.
Hi ,
Thanks,
The problem is that i dont know the model beforehand, but all the models that use the helper has the same property.
The calling razor looks like this:
Hi Peter
What is the type of imageSlider and i.ImageItems?
IEnumerable<IPublishedContent>
orIEnumerable<IPublishedElement>
??It's hard to tell, but it's likely your img, implements
IPublishedContent
so you could probably use that as your type in your razor helper.??
If your razor helper, is rendering HTML would it be better to have another PartialView for rendering images, that you called like Html.Partial("RenderImagePartial", model);
rather than a razor helper?
or perhaps an extension method on HtmlHelper, so you could call it in your views like @Html.RenderImageHelper(model): https://docs.microsoft.com/en-us/aspnet/mvc/overview/older-versions-1/views/creating-custom-html-helpers-cs
regards
Marc
Hi,
yes, this is the way.
Then i can call it like this:
Thanks
is working on a reply...