Shane LeBlanc
Shane LeBlanc

Reputation: 2643

Using ViewBag properties for Html.Routelink link text

Attempting to use ViewBag when calling Html.RouteLink:

@{                             
    <li>@Html.RouteLink("Inbox (" + ViewBag.NewMessageCount + ")", 
                          "ViewInbox", 
                          new { pseudoName = User.Identity.Name }) | </li>
}

This results in an error:

System.Web.Mvc.HtmlHelper' has no applicable method named 'RouteLink' 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.

How can you use a property on the ViewBag value in the Link Text?

Upvotes: 2

Views: 777

Answers (1)

SLaks
SLaks

Reputation: 887459

As the error message tells you, you cannot use dynamic operations with extension methods.

Casting the ViewBag. expression to a non-dynamic type will fix the problem.

(string)(ViewBag.MyProperty)

Upvotes: 4

Related Questions