html.actionlink 字体颜色,HTML.ActionLink方法

博客内容介绍了如何在ASP.NET MVC中扩展HtmlHelper,创建一个自定义的ActionLink方法,支持更灵活的链接生成,包括传递表达式、链接文本、路由值和HTML属性。这个扩展方法可以帮助开发者更方便地在视图中构建链接。
摘要由CSDN通过智能技术生成

If you want to go all fancy-pants, here's how you can extend it to be able to do this:

@(Html.ActionLink(x => x.Details(), article.Title, new { id = article.ArticleID }))

You will need to put this in the System.Web.Mvc namespace:

public static class MyProjectExtensions

{

public static MvcHtmlString ActionLink(this HtmlHelper htmlHelper, Expression> expression, string linkText)

{

var urlHelper = new UrlHelper(htmlHelper.ViewContext.RequestContext, htmlHelper.RouteCollection);

var link = new TagBuilder("a");

string actionName = ExpressionHelper.GetExpressionText(expression);

string controllerName = typeof(TController).Name.Replace("Controller", "");

link.MergeAttribute("href", urlHelper.Action(actionName, controllerName));

link.SetInnerText(linkText);

return new MvcHtmlString(link.ToString());

}

public static MvcHtmlString ActionLink(this HtmlHelper htmlHelper, Expression> expression, string linkText, object routeValues)

{

var urlHelper = new UrlHelper(htmlHelper.ViewContext.RequestContext, htmlHelper.RouteCollection);

var link = new TagBuilder("a");

string actionName = ExpressionHelper.GetExpressionText(expression);

string controllerName = typeof(TController).Name.Replace("Controller", "");

link.MergeAttribute("href", urlHelper.Action(actionName, controllerName, routeValues));

link.SetInnerText(linkText);

return new MvcHtmlString(link.ToString());

}

public static MvcHtmlString ActionLink(this HtmlHelper htmlHelper, Expression> expression, string linkText, object routeValues, object htmlAttributes) where TController : Controller

{

var urlHelper = new UrlHelper(htmlHelper.ViewContext.RequestContext, htmlHelper.RouteCollection);

var attributes = AnonymousObjectToKeyValue(htmlAttributes);

var link = new TagBuilder("a");

string actionName = ExpressionHelper.GetExpressionText(expression);

string controllerName = typeof(TController).Name.Replace("Controller", "");

link.MergeAttribute("href", urlHelper.Action(actionName, controllerName, routeValues));

link.MergeAttributes(attributes, true);

link.SetInnerText(linkText);

return new MvcHtmlString(link.ToString());

}

private static Dictionary AnonymousObjectToKeyValue(object anonymousObject)

{

var dictionary = new Dictionary();

if (anonymousObject == null) return dictionary;

foreach (PropertyDescriptor propertyDescriptor in TypeDescriptor.GetProperties(anonymousObject))

{

dictionary.Add(propertyDescriptor.Name, propertyDescriptor.GetValue(anonymousObject));

}

return dictionary;

}

}

This includes two overrides for Route Values and HTML Attributes, also, all of your views would need to add: @using YourProject.Controllers or you can add it to your web.config

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值