All,
Getting to grips with ASP.NET MVC. So far, so good, but this one is a little nuts.
I have a view model that contains a dictionary of attributes for a hyperlink, used like this:
menu = model variable
Html.ActionLink(Html.Encode(menu.Name), Html.Encode(menu.Action), Html.Encode(menu.Controller), menu.Attributes, null)
The problem is the position of "menu.Attributes" expects an object in the form:
new { Name = "Fred", Age=24 }
From what I can tell, this anonymous object is actually converted to a dictionary via reflection anyway BUT you can't pass a dictionary to it in the first place!!!
The Html generated for the link simply shows the dictionary type.
How on earth do I get round this? The whole point is that its general and the controller can have set the menu.Attributes previously....
Based on a post below I tried the following:
Html.ActionLink(Html.Encode(menu.Name), Html.Encode(menu.Action), Html.Encode(menu.Controller), new RouteValueDictionary(menu.Attributes), new Dictionary<string,object>())
but this still doesn't work (I guess the code internally calls the generic method that takes objects?). The above (and my original solution of passing a dictionary to the 4th paramater produces a HTML similar to this:
<a href="/EditRole?Comparer=System.Collections.Generic.GenericEqualityComparer%601%5BSystem.String%5D&Count=1&Keys=System.Collections.Generic.Dictionary%602%2BKeyCollection%5BSystem.String%2CSystem.String%5D&Values=System.Collections.Generic.Dictionary%602%2BValueCollection%5BSystem.String%2CSystem.String%5D">EditDocumentRoles</a>
i.e. it's using reflection and working things out completely wrong...
The suggestions on how to fix worked for me in MVC3. Example usage:
IDictionary<string, object> routeValues = new Dictionary<string, object>();
routeValues.Add("EmployeeID", 1);
@Html.ActionLink("Employee Details", "EmployeeDetails", "Employee", new RouteValueDictionary(routeValues), null);