I have a Dictionary<string, string>
with key values that are used for the HTML.ActionLink
links. Mostly the TKeys
are used but if there is a TValue
that's not empty I would like to use that instead. The reason is that for UI reasons some words work better than the controller action name, like GetUserProfile is better displayed as User Profile.
Html.ActionLink
is throwing this error:
CS1928: '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, string, object, object)' has some invalid arguments
I'm pretty sure that it's because it's seeing the Dictionary<string, string>
type and not the string
type. So is there a way to convert the Dictionary TKey/TValue
into a string
type to satisfy what it's asking for? Or another way I'm not thinking to accomplish the same task?
@{
Dictionary<string, string> nav = new Dictionary<string, string>() {{ "Email", "" }, { "Username", "" }, { "EditProfile", "Edit Profile" }};
}
@foreach (var navItem in nav)
{
if (navItem.Value.IsEmpty())
{
<li>
@Html.ActionLink(navItem.Key, navItem, "ControllerName")
</li>
}
else
{
<li>
@Html.ActionLink(navItem.Value, navItem, "ControllerName")
</li>
}
}
You are using ActionLink(HtmlHelper, String, String, String)
with the second argument (count after the HtmlHelper
as it is an extension method) that expects a string
value.
While you are providing the value of KeyValuePair<string, string>
to the second argument. I believe what you want is to provide the navItem.Key
as actionName
.
@foreach (var navItem in nav)
{
if (navItem.Value.IsEmpty())
{
<li>
@Html.ActionLink(navItem.Key, navItem.Key, "ControllerName")
</li>
}
else
{
<li>
@Html.ActionLink(navItem.Value, navItem.Key, "ControllerName")
</li>
}
}