在ASP的最新(RC1)版本中。NET MVC,我怎么得到Html。ActionLink渲染为按钮或图像而不是链接?
当前回答
@using (Html.BeginForm("DeleteMember", "Member", new { id = Model.MemberID }))
{
<input type="submit" value="Delete Member" onclick = "return confirm('Are you sure you want to delete the member?');" />
}
其他回答
如果你不想使用链接,使用按钮。你也可以添加图片到按钮:
<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"执行你的动作而不是提交表单。
使用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" })
这是我在没有脚本的情况下做到的:
@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>
}
这是一个很晚的回答,但这是我如何将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>
}
希望这对大家有所帮助
我喜欢像这样使用Url.Action()和Url.Content():
<a href='@Url.Action("MyAction", "MyController")'>
<img src='@Url.Content("~/Content/Images/MyLinkImage.png")' />
</a>
严格来说,Url。内容只在路径不是您问题答案的一部分时才需要。
感谢@BrianLegg指出这应该使用新的Razor视图语法。示例已相应更新。
推荐文章
- 把内容放在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的主要缺点是什么?