Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting error while using @Html.ActionLink() [duplicate]

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="">[email protected]</a></li>  
            }
        }
    </ul>
</form>
like image 279
Sara Avatar asked Nov 30 '22 03:11

Sara


2 Answers

Type cast item.Key to string

@Html.ActionLink((string)item.Key,"ExpandFolder","Home",new {path=item.Key },null)
like image 186
Satpal Avatar answered Dec 02 '22 15:12

Satpal


@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.

like image 39
whoah Avatar answered Dec 02 '22 17:12

whoah