在ASP的最新(RC1)版本中。NET MVC,我怎么得到Html。ActionLink渲染为按钮或图像而不是链接?


当前回答

如果你不想使用链接,使用按钮。你也可以添加图片到按钮:

<button type="button" onclick="location.href='@Url.Action("Create", "Company")'" >
   Create New
   <img alt="New" title="New" src="~/Images/Button/plus.png">
</button>

Type ="button"执行你的动作而不是提交表单。

其他回答

延迟响应,但你可以保持简单,并应用CSS类htmlAttributes对象。

<%= Html.ActionLink("Button Name", "Index", null, new { @class="classname" }) %>

然后在样式表中创建一个类

a.classname
{
    background: url(../Images/image.gif) no-repeat top left;
     display: block;
     width: 150px;
     height: 150px;
     text-indent: -9999px; /* hides the link text */
}

我喜欢像这样使用Url.Action()和Url.Content():

<a href='@Url.Action("MyAction", "MyController")'>
    <img src='@Url.Content("~/Content/Images/MyLinkImage.png")' />
</a>

严格来说,Url。内容只在路径不是您问题答案的一部分时才需要。

感谢@BrianLegg指出这应该使用新的Razor视图语法。示例已相应更新。

如果你不想使用链接,使用按钮。你也可以添加图片到按钮:

<button type="button" onclick="location.href='@Url.Action("Create", "Company")'" >
   Create New
   <img alt="New" title="New" src="~/Images/Button/plus.png">
</button>

Type ="button"执行你的动作而不是提交表单。

对于Material Design Lite和MVC:

<a class="mdl-navigation__link" href='@Url.Action("MyAction", "MyController")'>Link Name</a>
<li><a href="@Url.Action(  "View", "Controller" )"><i class='fa fa-user'></i><span>Users View</span></a></li>

显示带有链接的图标