Search code examples
asp.net-mvchyperlinkviewviewdata

ViewData as a hyperlink


I an ASP.NET MVC we can pass some data via a ViewData and then show it on a page:

<%: ViewData["Foo"]%>

But how to make a hyperlink out of it?

Something like following:

<%: Html.ActionLink(ViewData["Foo"], "Index", "Home") %>

Solution

  • Cast it to string:

    Html.ActionLink((string)ViewData["Foo"], "Index", "Home")
    

    In general, however, try to avoid using ViewData and use a strongly typed ViewModel instead. (Thus, you would have avoided the problem in this question, btw).