use*_*634 3 asp.net-mvc asp.net-mvc-3
我在干净的HTML中有这个链接.我想把它变成一个MVC ActionLink.但这怎么可能呢.我无法理解,似乎没有简单的方法.要么?
<li class="active"><a href="#"><i class="icon-home icon-large"></i> Dashboard</a></li>
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
Ste*_*rup 13
你有几个选择这样做.
选项1:
<li class="active"><a href="@Url.Action("YourAction", "YourController")"><i class="icon-home icon-large"></i> Dashboard</a></li>
Run Code Online (Sandbox Code Playgroud)
选项2:
为HtmlHelper编写自己的扩展方法,它接收String/MvcHtmlString并将其放入链接标记中,而不对其进行html编码.
public static MvcHtmlString SpecialActionLink(this HtmlHelper html, String action, String controller, String innerHtml) {
TagBuilder tag = new TagBuilder("a");
tag.MergeAttribute("href", new UrlHelper(html.ViewContext.RequestContext).Action(action, controller));
tag.InnerHtml = innerHtml;
return new MvcHtmlString(tag.ToString(TagRenderMode.Normal));
}
Run Code Online (Sandbox Code Playgroud)
(没有测试过,但应该告诉你如何做到这一点.)
<li class="active">
@Html.ActionLink("Dashboard", //Title
"Index", // Action
"Home", // Controller
new { @class = "home-icon"} // htmlAttributes
)
</li>
.home-icon{ /*Some CSS to set the background image (not tested) */
padding:16px;
background:transparent url('path-to-image.png') .... ;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14377 次 |
| 最近记录: |