C# 自定义HtmlHelper(ActionLink中的图像)
我已经看到了一些不同的方法,但并不是我想要的。我目前有一个自定义的HtmlHelper,它将“class='currentPage'”添加到操作链接中,以突出显示导航系统中的当前页面C# 自定义HtmlHelper(ActionLink中的图像),c#,html,css,asp.net-mvc-3,C#,Html,Css,Asp.net Mvc 3,我已经看到了一些不同的方法,但并不是我想要的。我目前有一个自定义的HtmlHelper,它将“class='currentPage'”添加到操作链接中,以突出显示导航系统中的当前页面 public static MvcHtmlString MenuItem( this HtmlHelper htmlHelper, string text, string action, string controlle
public static MvcHtmlString MenuItem(
this HtmlHelper htmlHelper,
string text,
string action,
string controller
)
{
string value;
var routeData = htmlHelper.ViewContext.RouteData;
var currentAction = routeData.GetRequiredString("action");
var currentController = routeData.GetRequiredString("controller");
if (string.Equals(currentAction, action, StringComparison.OrdinalIgnoreCase) &&
string.Equals(currentController, controller, StringComparison.OrdinalIgnoreCase))
{
value = htmlHelper.ActionLink(text, action, new { controller = controller }, new { @class = "currentPage" }).ToHtmlString();
return MvcHtmlString.Create(value.ToString());
}
value = htmlHelper.ActionLink(text, action, controller).ToHtmlString();
return MvcHtmlString.Create(value.ToString());
}
这工作得很好,但我有一个管理区,我也有一个与每个菜单项相关联的图像。图像位于链接内,如下所示:
<li><a href="/Admin/Blog"><img src="/Content/images/icons/page_edit.png" alt="" /> Blog</a></li>
我想为“MenuItem”创建一个覆盖方法,将图像添加到ActionLink中,但我有点为难。目前我有以下内容,将img标签放在外面
public static MvcHtmlString MenuItem(
this HtmlHelper htmlHelper,
bool isAdmin,
string text,
string action,
string controller
)
{
string value;
var routeData = htmlHelper.ViewContext.RouteData;
var currentAction = routeData.GetRequiredString("action");
var currentController = routeData.GetRequiredString("controller");
value = "<img src='/Content/images/admin_icons/" + text + ".png' alt='' /> ";
if (string.Equals(currentAction, action, StringComparison.OrdinalIgnoreCase) &&
string.Equals(currentController, controller, StringComparison.OrdinalIgnoreCase))
{
value += htmlHelper.ActionLink(text, action, new { controller = controller }, new { @class = "currentPage" }).ToHtmlString();
}
else
{
value += htmlHelper.ActionLink(text, action, controller).ToHtmlString();
}
return MvcHtmlString.Create(value.ToString());
}
publicstaticmvchtmlstring菜单项(
这个HtmlHelper HtmlHelper,
布尔·伊萨明,
字符串文本,
弦作用,
字符串控制器
)
{
字符串值;
var routeData=htmlHelper.ViewContext.routeData;
var currentAction=RoutedData.GetRequiredString(“操作”);
var currentController=RoutedData.GetRequiredString(“控制器”);
value=“”;
if(string.Equals(currentAction、action、StringComparison.OrdinalIgnoreCase)&&
string.Equals(currentController、controller、StringComparison.OrdinalIgnoreCase))
{
value+=htmlHelper.ActionLink(文本,操作,新的{controller=controller},新的{@class=“currentPage”});
}
其他的
{
value+=htmlHelper.ActionLink(文本、操作、控制器).ToHtmlString();
}
返回MvcHtmlString.Create(value.ToString());
}
有什么想法吗?我会使用UrlHelper生成URL,然后格式化字符串。我的示例不涉及添加条件类,但这应该很容易包含
var urlHelper = new UrlHelper(htmlHelper.ViewContext.RequestContext);
var url = urlHelper.Action(action, controller);
var imgUrl = urlHelper.Content("~/Content/images/icons/page_edit.png");
var html = string.Format("<li><a href=\"{0}\"><img src=\"{2}\" alt=\"\" />{1}</a></li>", url, text, imgUrl);
return new MvcHtmlString(html);
var urlHelper=newurlhelper(htmlHelper.ViewContext.RequestContext);
var url=urlHelper.Action(Action,controller);
var imgUrl=urlHelper.Content(“~/Content/images/icons/page_edit.png”);
var html=string.Format(“”,url,文本,imgUrl);
返回新的MvcHtmlString(html);
我将使用UrlHelper生成URL,然后格式化字符串。我的示例不涉及添加条件类,但这应该很容易包含
var urlHelper = new UrlHelper(htmlHelper.ViewContext.RequestContext);
var url = urlHelper.Action(action, controller);
var imgUrl = urlHelper.Content("~/Content/images/icons/page_edit.png");
var html = string.Format("<li><a href=\"{0}\"><img src=\"{2}\" alt=\"\" />{1}</a></li>", url, text, imgUrl);
return new MvcHtmlString(html);
var urlHelper=newurlhelper(htmlHelper.ViewContext.RequestContext);
var url=urlHelper.Action(Action,controller);
var imgUrl=urlHelper.Content(“~/Content/images/icons/page_edit.png”);
var html=string.Format(“”,url,文本,imgUrl);
返回新的MvcHtmlString(html);
var html=string.Format(“”,url,文本,imgUrl);“索引(从零开始)必须大于或等于零,并且小于参数列表的大小。”此格式是否正确?已解决此问题。。。我没看到数字被弄错了。非常好,谢谢!var html=string.Format(“