I noticed that I use a code snippet in my template files very often. So to minimze code duplication I would like to make razor helper method of it. I would like it take the current model as a parameter. So my plan in to put it in "App_Code" to make it avaible to all template files. But when I us the following code:
I get the error message: "IPublishedContent could not be found found". I have tried to use some @using statements but I can't figure it out. Anything advice would help right now!
Note: As of writing this, I noticed that I could use a partial view, passing in the model to get the result I want. But could always be useful to know for the future!
I like extensions so I will show you how to do it using extensions. If you want to put your code in App_Code, create the following class in this folder.
using System.Web.Mvc;
using Umbraco.Core.Models.PublishedContent;
public static class MyExtension
{
public static MvcHtmlString GetTag(this IPublishedContent model)
{
TagBuilder h1 = new TagBuilder("h1");
h1.InnerHtml = model.Name;
return MvcHtmlString.Create(h1.ToString());
}
public static MvcHtmlString GetText(this IPublishedContent model)
{
return MvcHtmlString.Create(model.Name);
}
}
This class contains two extensions. The first generates an h1 tag and puts the page name inside, and the second returns just the name of the page. In order to use it you will have to type the following lines
Using IPublishedContent in razor helper method
Hello everyone!
I noticed that I use a code snippet in my template files very often. So to minimze code duplication I would like to make razor helper method of it. I would like it take the current model as a parameter. So my plan in to put it in "App_Code" to make it avaible to all template files. But when I us the following code:
I get the error message: "IPublishedContent could not be found found". I have tried to use some
@using
statements but I can't figure it out. Anything advice would help right now!Note: As of writing this, I noticed that I could use a partial view, passing in the model to get the result I want. But could always be useful to know for the future!
Thanks!
//Johannes
I like extensions so I will show you how to do it using extensions. If you want to put your code in App_Code, create the following class in this folder.
This class contains two extensions. The first generates an h1 tag and puts the page name inside, and the second returns just the name of the page. In order to use it you will have to type the following lines
I hope it helps.
Hi Aristotelis!
This is really useful! Thank you so much!
is working on a reply...
This forum is in read-only mode while we transition to the new forum.
You can continue this topic on the new forum by tapping the "Continue discussion" link below.