This project is read-only.

Error on Build: The name 'Html' does not exist in the current context

Jan 5, 2012 at 5:56 PM

Hello,

I created the following helper file under Areas/CMS/Views:

@* Generator: MvcHelper *@

@helper MenuLink(String text, ActionResult result, Boolean spot) {  
  if (spot) {
    <span>@text</span>
  } else {
    @Html.ActionLink(text, result)   
  }    
}

When I write @Html. I get intellisense as expected and I am able to type ActionLink with no problems.

Then I build the project and I get the following error: "The name 'Html' does not exist in the current context"

On Web.Config I have the following:

      <namespaces>
        <add namespace="System.Globalization" />
        <add namespace="System.Threading" />
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Routing" />
        <add namespace="MVCSite.Areas.CMS.Views" />
      </namespaces>

The error happens only when I build the project not when I type. Any idea why?

Thank You,

Miguel

Jan 5, 2012 at 6:14 PM

Does this work with a regular Mvc app to begin with? I don't think you're allowed to access the HtmlHelper in a Mvc helper. What you should be doing though is

@* Generator: MvcHelper *@

@helper MenuLink(this HtmlHelper helper, string text, ActionResult result, Boolean spot) {  
  if (spot) {
    <span>@text</span>
  } else {
    @helper.ActionLink(text, result)   
  }    
}

and invoke it as

   @Html.MenuLink(text, result, spot) from within your page.
Jan 5, 2012 at 6:48 PM

I tried something similar to what you wrote before but

  "helper.ActionLink(text, result)" does not render anything.

If I try to add @ as you wrote:

  "@helper.ActionLink(text, result)" I get the error "Invalid expression term '.'" on ActionLink.

Any idea?

Thank You,

Miguel

Jan 5, 2012 at 7:24 PM

Just changed it to:

@helper MenuLink(this HtmlHelper html, string text, ActionResult result, Boolean spot) {  
  if (spot) {
    @text
  } else {
    @html.ActionLink(text, result)   
  }    
}

It is working now.

Thank You,

Miguel