Note that there are some explanatory texts on larger screens.

plurals
  1. POSharing Helper Funactions across Views in ASP.NET MVC 3
    primarykey
    data
    text
    <p>I have a Helper method that I need to use across multiple views. I tried to using the following information: <a href="http://weblogs.asp.net/scottgu/archive/2011/05/12/asp-net-mvc-3-and-the-helper-syntax-within-razor.aspx" rel="nofollow">http://weblogs.asp.net/scottgu/archive/2011/05/12/asp-net-mvc-3-and-the-helper-syntax-within-razor.aspx</a>. I have defined this Helper method in /App_Code/Helpers.cshtml. My views are located at</p> <pre><code>/resources/_Layout.cshtml /Views/Index.cshtml /Views/Home/Index.cshtml </code></pre> <p>If I reference my Helper function in _Layout.cshtml everything works fine. However, if I reference my Helper function in the other views, I get a runtime error. The runtime error says:</p> <p>CS0103: The name 'MyFunction' does not exist in the current context</p> <p>My Helper method looks like the following:</p> <pre><code>/App_Code/Helpers.cshtml ------------------------ @helper MakeUrlRelative(string url) { @MyNamespace.MyClass.MakeUrlRelative(HttpContext.Current.Request, url) } </code></pre> <p>When I reference it in /Views/Index.cshtml, I use it like this:</p> <pre><code>&lt;link rel="stylesheet" href="@MakeUrlRelative("/resources/css/app.css")" /&gt; </code></pre> <p>When I reference it in /Views/Home/Index.cshtml</p> <pre><code>&lt;a href="@MakeUrlRelative("/home/page2")"&gt;click me&lt;/a&gt; </code></pre> <p>As you can see, I'm referencing the MakeUrlRelative method the same way. Its like the Helpers.cshtml file isn't accessible from /Views/Home/Index.cshtml. What would cause this?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload