在ASP的最新(RC1)版本中。NET MVC,我怎么得到Html。ActionLink渲染为按钮或图像而不是链接?
当前回答
使用bootstrap,这是创建控制器动作链接的最短和最干净的方法,该控制器动作以动态按钮的形式出现:
<a href='@Url.Action("Action", "Controller")' class="btn btn-primary">Click Me</a>
或者使用Html helper:
@Html.ActionLink("Click Me", "Action", "Controller", new { @class = "btn btn-primary" })
其他回答
关于如何创建一个显示为图像的链接,似乎有很多解决方案,但没有一个能使它看起来像一个按钮。
我只找到了一个好办法。这有点俗气,但很管用。
你要做的就是创建一个按钮和一个单独的动作链接。使用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>
显示带有链接的图标
这是我在没有脚本的情况下做到的:
@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>
}
你不能用Html.ActionLink这样做。你应该使用Url。RouteUrl并使用该URL来构造所需的元素。
如果你不想使用链接,使用按钮。你也可以添加图片到按钮:
<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"执行你的动作而不是提交表单。
推荐文章
- 把内容放在HttpResponseMessage对象?
- asp.net MVC中的@RenderSection是什么
- 在哪里放置AutoMapper.CreateMaps?
- 我如何提高ASP。NET MVC应用程序性能?
- 如何在剃刀视图上引用.css文件?
- 获取“JSON请求太大,无法反序列化”
- 存储库和服务层的区别?
- 在ASP中实现请求节流的最佳方法。净MVC吗?
- 如何添加ID属性Html.BeginForm()在asp.net mvc?
- 什么是MvcHtmlString,什么时候我应该使用它?
- 生成错误:必须向系统添加引用。运行时
- Visual Studio 2012 Web Publish不复制文件
- 在ASP中使用jQuery渲染局部视图。NET MVC
- 如何返回一个文件(FileContentResult)在ASP。净WebAPI
- Java Server Faces 2.0的主要缺点是什么?