I am writing a custom helper to handle nested navigation menu. I am having some trouble with passing a set of arrays (or dictionary) to the function.
Below is the Razor call to the ActionMenuItem
@Html.ActionMenuItem("All Reports", "index", "report", "icon-bar-chart", "last", new {"title" = "Report 1", "action" = "report1"}, new {"title" = "Report 2", "action" = "report2"})
public static MvcHtmlString ActionMenuItem(this HtmlHelper htmlHelper, String linkText, String actionName, String controllerName, String iconType = null, string classCustom = null, params Dictionary<string, string> subMenu)
My function works well, up until the dictionary items. I am able to generate a single level menu, but trying to get it to work with nested menus.
Any help, and lessons is much appreciated!
Thank you,
RD
Could you do something like this:
public static MvcHtmlString ActionMenuItem(
this HtmlHelper htmlHelper,
String linkText,
String actionName,
String controllerName,
String iconType = null,
string classCustom = null,
params KeyValuePair<string, string>[] subMenus)
{ ... }
var dict = new Dictionary<string, string>()
{
{ "a", "b" },
{ "c", "d" },
};
*.ActionMenuItem(*, *, *, *, *, dict.ToArray());
You can't declare Dictionary<TKey, TValue>
as parameter array using params
keyword.
According to c# specification:
A parameter declared with a
params
modifier is a parameter array. If a formal parameter list includes a parameter array, it must be the last parameter in the list and it must be of a single-dimensional array type .
Dictionary
is not a single-dimensional array .
You could create a class with two properties: Title
and Action
and change parameter type to MyClass[]
instead of a dictionary.
Try something like this:
@Html.ActionMenuItem(
"All Reports",
"index",
"report",
"icon-bar-chart",
"last",
new Dictionary<string, string>[]
{
new Dictionary<string, string>()
{
{ "title", "Report 1" },
{ "action", "report1" }
},
new Dictionary<string, string>()
{
{ "title", "Report 2" },
{ "action", "report2" }
}
} )
public static MvcHtmlString ActionMenuItem(
this HtmlHelper htmlHelper,
string linkText,
string actionName,
string controllerName,
string iconType = null,
string classCustom = null,
params Dictionary<string, string>[] subMenu)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.