gpt4 book ai didi

c# - MVC 自定义助手附加 htmlAttributes

转载 作者:行者123 更新时间:2023-11-30 23:10:40 27 4
gpt4 key购买 nike

我有一个自定义帮助程序类来突出显示所选的菜单项,

<li class="nav-item">
@Html.MenuLink("Contact000", "Contact", "Home", new { @class = "nav-link" })
</li>

如果所选条件为真,我想使用辅助 html 属性并附加到 CSS。我见过Similiar question但无法使用

找到问题
var attrs = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes);

这是我的代码

public static MvcHtmlString MenuLink(this HtmlHelper helper, string text, string action, string controller, object htmlAttributes)
{
var routeData = helper.ViewContext.RouteData.Values;
var currentController = routeData["controller"];
var currentAction = routeData["action"];

var attrs = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes);

if (String.Equals(action, currentAction as string,
StringComparison.OrdinalIgnoreCase)
&&
String.Equals(controller, currentController as string,
StringComparison.OrdinalIgnoreCase))
{
if (attrs.ContainsKey("class"))
{
attrs["class"] += " " + "currentMenuItem";
}

return helper.ActionLink(text, action, controller, attrs);
}

return helper.ActionLink(text, action, controller, htmlAttributes);
}

使用

 var attrs = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes);

我可以附加到属性。

当我返回 helper.actionlink 时,attrs 没有被正确解析,下面是生成的 html,

<li class="nav-item">
<a Count="1" Keys="System.Collections.Generic.Dictionary`2+KeyCollection[System.String,System.Object]" Values="System.Collections.Generic.Dictionary`2+ValueCollection[System.String,System.Object]" href="/Home/Contact?Length=4">Contact000</a>
</li>

我正在尝试获取以下内容,

<li class="nav-item">
<a class="nav-link currentMenuItem" href="/Home/Contact">Contact000</a>
</li>

如何将 attrs 转换回 htmlAttributes 以使助手工作?

最佳答案

您可以创建一个 RouteValueDictionary 使用 object htmlAttributes .然后你可以像附加任何其他值一样附加一个值 IDictionary<TKey, TValue> 界面。

IDictionary<string, object> attrs = new RouteValueDictionary(htmlAttributes);

string key = "key";
string oldValue;
if (attrs.TryGetValue(key, out oldValue))
{
// append to value
attrs[key] = oldValue + "string to append";
}
else
{
// key not in attrs, handle accordingly
}

完整示例:

using System.Web.Routing;

public static MvcHtmlString MenuLink(this HtmlHelper helper,
string text, string action, string controller, object htmlAttributes)
{
var routeData = helper.ViewContext.RouteData.Values;
var currentController = routeData["controller"];
var currentAction = routeData["action"];

IDictionary<string, object> attrs = new RouteValueDictionary(htmlAttributes);

if (String.Equals(action, currentAction as string,
StringComparison.OrdinalIgnoreCase)
&& String.Equals(controller, currentController as string,
StringComparison.OrdinalIgnoreCase))
{
string clazz;
if (attrs.TryGetValue("class", out clazz))
{
attrs["class"] = clazz + " " + "currentMenuItem";
}
// do you need to handle the case when there is no "class" attribute?

return helper.ActionLink(text, action, controller, attrs);
}

return helper.ActionLink(text, action, controller, htmlAttributes);
}

编辑:我发现您的问题的实际问题,您使用的是错误的 Html.ActionLink过载。

您目前正在使用 ActionLink(HtmlHelper, String, String, Object, Object) 过载,当你应该使用 ActionLink(HtmlHelper, String, String, String, RouteValueDictionary, IDictionary<String, Object>) 过载。

helper.ActionLink(text, action, controller, routeValues: null, htmlAttributes: attrs);

关于c# - MVC 自定义助手附加 htmlAttributes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45225845/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com