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


当前回答

你可以说我过于简单,但我就是这么做的:

<a href="<%: Url.Action("ActionName", "ControllerName") %>">
    <button>Button Text</button>
</a>

你只需要处理超链接突出显示。我们的用户喜欢它:)

其他回答

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

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

这是我在没有脚本的情况下做到的:

@using (Html.BeginForm("Action", "Controller", FormMethod.Get))
{
    <button type="submit" 
            class="btn btn-default" 
            title="Action description">Button Label</button>
}

相同,但有参数和确认对话框:

@using (Html.BeginForm("Action", "Controller", 
        new { paramName = paramValue }, 
        FormMethod.Get, 
        new { onsubmit = "return confirm('Are you sure?');" }))
{
    <button type="submit" 
            class="btn btn-default" 
            title="Action description">Button Label</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();
 }

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

URL . action()将为您提供大多数Html重载的纯URL。ActionLink,但我认为URL-from-lambda功能只能通过Html使用。ActionLink到目前为止。希望他们会给Url添加类似的重载。在某种程度上采取行动。

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

显示带有链接的图标