I’m trying to add a navigational menu for my project which uses the ASP.NET framework and C# programming language. My solution is to create a widget which can populate a partial view when called from the master page.
In the widget’s action method, how do I add Links or Controller-Action combinations to the ViewDataDictionary?
— Edit : Here is my code, after your suggestion
public class NavController : Controller
{
public ActionResult Menu()
{
List<ActionLink> navLinks = new List<ActionLink>();
navLinks.Add(new ActionLink() { Text = "Home", ActionName = "Index", ControllerName = "Home" });
return View(navLinks);
}
}
The partial view file looks like the following:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<List<Microsoft.Web.Mvc.Controls.ActionLink>>" %>
<% foreach(var link in Model) { %>
<%= link %>
<%} %>
But the output of all this is simply ‘Microsoft.Web.Mvc.Controls.ActionLink’ and not the link which I want.
Thanks.
The actionlink is a mvc control of some sort that would be the result of parsing a tag and would then be added to the controls collection of the page.
You have 2 options to achieve your goal, pass in the complete links ( i.e. the fully formed anchor tag ) which you could generate in your controller using Html.ActionLink( linkText, actionName) or pass the details of the link to your partial and use those details to generate the links ( i.e. pass an enumeration of elements containing the details of your links ).
Personally I prefer the second approach as it separates the navigation logic from the rendering.
Somewhere in your code …
Somewhere in your controller …
Somewhere in your view …