4

Getting error as following..

Compiler Error Message: CS1973: 'System.Web.Mvc.HtmlHelper' has no applicable method named 'ActionLink' but appears to have an extension method by that name. Extension methods cannot be dynamically dispatched. Consider casting the dynamic arguments or calling the extension method without the extension method syntax.

In MVC application,I'm trying to use a dictionary entry's key as link text of a link, but im getting error.

This is the line where i'm getting error...i tried replacing item.Key with @item.Key and various other ways too.

@Html.ActionLink(item.Key,"ExpandFolder","Home",new {path=item.Key },null)

My entire view code looks like this...

<form id="ViewFiles" method="post"  action="Index">
    <ul>
        @foreach (var item in @ViewBag.filesFolders)
        {
            if (item.Value == "folder")
            {
                <li>
                    @Html.ActionLink(item.Key,"ExpandFolder","Home",new {path=item.Key },null)
                </li>  
            }
            else if (item.Value == "file")
            {
                <li><a href="">FILES-@item.Key</a></li>  
            }
        }
    </ul>
</form>
abatishchev
  • 98,240
  • 88
  • 296
  • 433
Sara
  • 93
  • 1
  • 10

2 Answers2

14

Type cast item.Key to string

@Html.ActionLink((string)item.Key,"ExpandFolder","Home",new {path=item.Key },null)
Satpal
  • 132,252
  • 13
  • 159
  • 168
  • I have this same problem but when I cast it, It says Cannot convert type 'long' to 'string'.. any idea on how to work around this? `@Html.ActionLink((string)@item.ItemID, "Viewitem", new { itemID = item.ItemID})` – whisk Jul 08 '20 at 13:31
6

@ViewBag.filesFolders

Here is the problem. HtmlHelper returns an error description:

Extension methods cannot be dynamically dispatched.

Your ViewBag.filesFolders is dynamically typed viewbag, so it cause problem.

You should use strongly-typed ViewModel (and better - IEnumerable<T>) to show yours filesFolders.

Other solution is to cast your item.Key to string type, so it will looks like:

@Html.ActionLink((string)item.Key, "ExpandFolder", "Home", new {path=item.Key }, null)

But as I said before, I recommend using strongly typed View Models in future.

whoah
  • 4,363
  • 10
  • 51
  • 81