TreeViewFor method not found

Aug 22, 2012 at 7:33 PM

Hi,

I'm trying to show product categories using the TreeView component in a Razor view in MVC4. The model class is a ProductCategoryDto. It contains a collection of (please sit down) product categories. A product category has a list of subcategories that also needs to be shown in the treeview.

public class ProductCategoryDto
{
    public List<ProductCategory> Categories { get; set; }
}

The problem that I have is that the TreeviewFor method could not be found: System.Web.Mvc.HtmlHelper<Web.External.Dto.ProductCategoryDto>' does not contain a definition for 'TreeviewFor' and no extension method 'TreeviewFor' accepting a first argument of type 'System.Web.Mvc.HtmlHelper<Web.External.Dto.ProductCategoryDto>' could be found (are you missing a using directive or an assembly reference?)
IntelliSense doesn't come up with the TreeviewFor method. Should it?
I never worked with the toolkit before, I downloaded it yesterday so maybe there's some config that's not right or something like that. Is the type of my model class ok? Should it be of a certain type?
@model AdventureWorks.Web.External.Dto.ProductCategoryDto

<div>
    @Html.TreeviewFor(...
</div>
Regards,
Nils
Coordinator
Aug 23, 2012 at 2:45 PM

Surely is a problem of importing the needed namespaces. If you installed via nuget all needed imports should be done automatically in the web config. If you just copied the dll from codeplex, please refer to Installation Without the Nuget packages.

That said please notice that the version that works with MVC4 RTM has not bee released yet and it will be delivered in a few days. The 2.2 release is just compatible with the Mvc4 RC runninng with .net 4.0. 

In a few days we will release Mv4 RTM compatible versions both for .Net 4.0 and 4.5.

Nov 16, 2012 at 1:37 PM

This is strange, I'm seeing the same thing as Nils. Just downloaded the latest version via NuGet into a new project. If I type this into a Razor view:

@Html.TreeViewFor(m => m.

There is no IntelliSense after typing the "."

After pasting this into the view:

@Html.TreeViewFor(m => m.Results, i=> null, ExternalContainerType.span, "filetree treeview-red", null, null, null, null, null, TreeViewMode.InitializeDisplay, null, null, null)

Then I can go back and delete ".Results" and then type the "." again and there's IntelliSense.

Very odd. I would say this is an issue with Visual Studio, but I've never seen this behavior before. Or maybe I just haven't noticed it before. That method sure does have a lot of parameters.

Coordinator
Nov 17, 2012 at 12:42 AM

This is a general problem of how Razor Views work. All HtmlHelpers have generics in they definition. Now when the class the generic is boud to belongs to a namespace that is not declared in any @using clause the Visual Studio intellisense is not able to work properly. So you should include in @using clauses all classes involved in the tree definition. In your case the namespace where the types of all nodes are defined should be included in a @using instruction in  the Razor View

Nov 17, 2012 at 12:49 AM
Edited Nov 17, 2012 at 12:50 AM

No, the using's are fine. I didn't have to change those at all. It apparently has something to do with filling in the whole method call. I do suspect it's a problem with VS's Razor editor; it does do some funky things compared to the C# editor.

Coordinator
Nov 17, 2012 at 11:39 AM

You are right also putting all @using sometimes creates the problem. The problem appears connected with the algorithm that computes the bindings between generics and actual types. The Razor VS intellisense algorithm sometimes fails, that is it is not able to computes that bindings while doing suggestions. Once you have written the right thing you give it more infos and it is able to works properly. However, what is strange is that behaviour appears randomly.