Samuel
Samuel

Reputation: 12351

Using html helpers in Razor web helper with Telerik

I try to create an html helper with a control from Telerik but to do that, I must have this directive at the top of the file:

@using Telerik.Web.Mvc.UI

The problem is in html helper, when I type '@using' and then try to typed 'Telerik', visual studio tells me that the namespace don't exist even if I already use this directive in many other places in the same project.

enter image description here

As you can see with the words in red, Telerik namespace produce errors; 'Cannot resolve symbol ...

This is the same code in one of my EditorTemplates folder of the same project but in this case, without errors:

enter image description here

Is it because it's not possible to have control other than the MVC framework or I missed something?

Thank you.

EDIT:

After searching and with the help of Darin, I finally found the problem. You can look in the comments of Darin answer.

Also, this this my final code that works:

@using System.Web.Mvc
@using Telerik.Web.Mvc.UI

@helper IntegerTextBox(System.Web.Mvc.HtmlHelper html, string id, int? value, bool enable = true)
{
   @(html.Telerik().IntegerTextBox()
        .Name(id)
        .InputHtmlAttributes(new {style = "width:200px"})
        .Spinners(false)
        .EmptyMessage("")
        .MaxValue(int.MinValue)
        .MaxValue(int.MaxValue)
        .Value(value)
        .Enable(enable)
        )
}

Like I said, you cannot use a @using directive to System.Web.Mvc to reduce the fully qualified type of the first parameter. I don't know why... 'HtmlHelper' looks like 'html helper' or '@helper' ... the word is in conflict with something in the framework of MVC? I don't know!? :(

Upvotes: 1

Views: 1662

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1039588

I guess that's due to the lousy Razor Intellisense support in Visual Studio. Your code should work fine when you run it. You could try reopening the view, restating VS, disable plugins and if it is your lucky day you might even get Intellisense working.

By the way, you could use the <namespaces> section of your ~/Views/web.config (not to confuse with ~/web.config) and add the Telerik.Web.Mvc.UI namespace. This will avoid you the need to add it to every single Razor view that needs it. Obviously for changes to take effect after modifying this file don't forget to reopen the razor page (as I already said Razor Intellisense sucks badly in VS).

Upvotes: 5

Related Questions