Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@helper and Url.Action

Tags:

asp.net-mvc

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?

like image 560
fran Avatar asked Feb 06 '13 00:02

fran


People also ask

What is a URL action?

A URL action is a hyperlink that points to a web page, file, or other web-based resource outside of Tableau. You can use URL actions to create an email or link to additional information about your data. To customize links based on your data, you can automatically enter field values as parameters in URLs.

What is difference between HTML ActionLink and URL action?

There is a difference. Html. ActionLink generates an <a href=".."></a> tag whereas Url. Action returns only an url.

What is the use of URL action in MVC?

Action(String, String, Object, String)Generates a fully qualified URL to an action method by using the specified action name, controller name, route values, and protocol to use.


1 Answers

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)
like image 103
Bassam Mehanni Avatar answered Sep 21 '22 12:09

Bassam Mehanni