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


当前回答

这是一个很晚的回答,但这是我如何将ActionLink变成一个按钮的。我们在我们的项目中使用Bootstrap,因为它很方便。不要介意@T,因为它只是我们使用的一个翻译器。

@Html.Actionlink("Some_button_text", "ActionMethod", "Controller", "Optional parameter", "html_code_you_want_to_apply_to_the_actionlink");

上面给出了一个如下图所示的链接:

localhost:XXXXX/Firms/AddAffiliation/F0500

在我看来:

@using (Html.BeginForm())
{
<div class="section-header">
    <div class="title">
        @T("Admin.Users.Users")
    </div>
    <div class="addAffiliation">
        <p />
        @Html.ActionLink("" + @T("Admin.Users.AddAffiliation"), "AddAffiliation", "Firms", new { id = (string)@WorkContext.CurrentFirm.ExternalId }, new { @class="btn btn-primary" })
    </div>
</div>

}

希望这对大家有所帮助

其他回答

你不能用Html.ActionLink这样做。你应该使用Url。RouteUrl并使用该URL来构造所需的元素。

一个简单的方法做你的Html。ActionLink变成一个按钮(只要你插入了BootStrap -你可能有)是这样的:

@Html.ActionLink("Button text", "ActionName", "ControllerName", new { @class = "btn btn-primary" })

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

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

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

这是一个很晚的回答,但这是我如何将ActionLink变成一个按钮的。我们在我们的项目中使用Bootstrap,因为它很方便。不要介意@T,因为它只是我们使用的一个翻译器。

@Html.Actionlink("Some_button_text", "ActionMethod", "Controller", "Optional parameter", "html_code_you_want_to_apply_to_the_actionlink");

上面给出了一个如下图所示的链接:

localhost:XXXXX/Firms/AddAffiliation/F0500

在我看来:

@using (Html.BeginForm())
{
<div class="section-header">
    <div class="title">
        @T("Admin.Users.Users")
    </div>
    <div class="addAffiliation">
        <p />
        @Html.ActionLink("" + @T("Admin.Users.AddAffiliation"), "AddAffiliation", "Firms", new { id = (string)@WorkContext.CurrentFirm.ExternalId }, new { @class="btn btn-primary" })
    </div>
</div>

}

希望这对大家有所帮助