2

I am using ASP.NET MVC 3, with the Razor engine. I have a partial view that contains one line:

@Html.ActionLink(ViewData["UserNameIfLoggedIn"], "Index", "Home")

This partial view is rendered in my _Layout.cshtml view. The snippet that calls the controller/action is this:

@{Html.RenderAction("UserLoggedIn", "User");}

I get a compilation error, stating:
'System.Web.Mvc.HtmlHelper' does not contain a definition for 'ActionLink' and the best extension method overload 'System.Web.Mvc.Html.LinkExtensions.ActionLink(System.Web.Mvc.HtmlHelper, string, string, object)' has some invalid arguments

I have made a similar call (same amount of arguments and same parameter datatypes) in another MVC application (a tutorial) and it executed just fine. What could be causing this?? Why is this not working now?

I know this is probably an extremely rookie MVC question, but I cannot figure this one out.

EDIT: The solution is this:

@Html.ActionLink(ViewData["UserNameIfLoggedIn"].ToString(), "Index", "Home")

I just needed to call the ToString() method to get the parameter as a string.

4
  • In your other call, was the first parameter to ActionLink a ViewData object? Commented Oct 28, 2011 at 20:28
  • @Xaisoft you know what, it is actually "Some Text Here" + ViewData["key"], in which case I'm guessing ViewData["key"] was cast to a string. Good catch. Commented Oct 28, 2011 at 20:45
  • No problem, I had a feeling it was something like that. Good luck. Commented Oct 28, 2011 at 23:43
  • Did you see this? stackoverflow.com/questions/5832692/… Commented May 21, 2014 at 1:42

2 Answers 2

4

You need a string as your first parameter in ActionLink, I am not sure but I think ViewData is a dictionary.

Try this just as a test

@Html.ActionLink("test", "Index", "Home")

If that works, then you need to loop through ViewData and get all the single values, and pass them as string. I am not sure what is your ViewData, though.

If you want just a single value, use ViewBag instead.

http://brendan.enrick.com/post/Difference-Between-ViewBag-and-ViewData-in-MVC-3.aspx

Sign up to request clarification or add additional context in comments.

4 Comments

ViewData is an object, not an enum
Thanks, I've read it's a dictionary of objects now. Thanks for that though!
Generally the best way of passing anything to view is using a view model. Then you're certain that everything is type saftly and you won't do an misspell ;)
ViewBag and ViewData are the same object, you can use either to put data in or get data out (try putting in data in ViewBag and then using the same name to get the data out of ViewData to see what I mean)
1

It's because ViewData / ViewBag is a dynamic that has no idea what type of data it holds, you need to unbox into a local variable before trying to use it

@{ var foo = ViewData["Title"]; }

@Html.ActionLink(foo.ToString(), "Index", "Home")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.