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