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


当前回答

使用FORMACTION

<input type="submit" value="Delete" formaction="@Url.Action("Delete", new { id = Model.Id })" />

其他回答

延迟响应,但你可以保持简单,并应用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 */
}

按照Mehrdad说的去做——或者像Stephen Walther在这里描述的那样,使用HtmlHelper扩展方法中的url helper,并创建自己的扩展方法,可以用来呈现所有的链接。

然后它将很容易渲染所有链接作为按钮/锚或任何你喜欢的-而且,最重要的是,你可以改变你的想法,当你发现你实际上更喜欢一些其他的方式制作你的链接。

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

<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使动作链接不可见。当您单击按钮时,它可以触发动作链接的单击事件。

<input type="button" value="Search" onclick="Search()" />
 @Ajax.ActionLink("Search", "ActionName", null, new AjaxOptions {}, new { id = "SearchLink", style="display:none;" })

function Search(){
    $("#SearchLink").click();
 }

每次添加一个看起来像按钮的链接时,这样做可能是一件痛苦的事情,但它确实实现了预期的结果。

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

显示带有链接的图标