5

I'm trying to extract some simple code into a separate helper function

    @helper ShowPath()
    {
    <p class="migas">
    <a href="@Url.Action("Index", "Home")">Home</a>
   &gt; <a href="@Url.Action("Index", "AboutUs")">About Us</a>
   &gt; Directory</p>
}

Works fine within the original cshtml file but the @Url.Action causes a compile error when I extract it to a separate file that I put within the App_Code folder.

Do I need to pass in a HTMLHelper from the calling page? If so, any ideas how?

0

2 Answers 2

6

you need to pass the UrlHelper to your method

@helper ShowPath(UrlHelper url)
{
    <p class="migas">
    <a href="@Url.Action("Index", "Home")">Home</a>
   &gt; <a href="@url.Action("Index", "AboutUs")">About Us</a>
   &gt; Directory</p>
}

and then from a view,

@YouAppCodeName.ShowPath(Url)
0

Try this

@using System.Web.Mvc
@using System.Web.Mvc.Html

@helper ShowPath(WebViewPage page)
{
  <p class="migas">
  <a href="@page.Url.Action("Index", "Home")">Home</a>
  &gt; <a href="@page.Url.Action("Index", "AboutUs")">About Us</a>
  &gt; Directory</p>
}

When you call it on the page send this as an argument. It can give you access to all the helpers if you need them in future.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.