我看到了关于@Html.Button()的参考资料,但是当我输入它时,智能感知却找不到这样的帮助程序……有下拉列表、隐藏、编辑器等,但没有按钮!为什么会这样呢?
我已经编写了自己的 HTMLButton 扩展,你可以使用它:
public static class HtmlButtonExtension
{
public static MvcHtmlString Button(this HtmlHelper helper,
string innerHtml,
object htmlAttributes)
{
return Button(helper, innerHtml,
HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes)
);
}
public static MvcHtmlString Button(this HtmlHelper helper,
string innerHtml,
IDictionary<string, object> htmlAttributes)
{
var builder = new TagBuilder("button");
builder.InnerHtml = innerHtml;
builder.MergeAttributes(htmlAttributes);
return MvcHtmlString.Create(builder.ToString());
}
}
namespace System.Web.Mvc.Html
中。 - Legends在MVC预览版3(而不是MVC3)中没有按钮帮助程序。
过去经常提到它,例如:
http://blog.wekeroad.com/blog/aspnet-mvc-preview-using-the-mvc-ui-helpers/
但自己编写很简单-基本上只需创建一个新的Html.ButtonFor并复制Html.LabelFor的源代码并更改输出,以创建一个<input type="button">
标签。
进一步解释已接受的答案,所以您可以绑定提交按钮到一个模型属性,但可以有不同的文本:
@Html.ButtonFor(m => m.Action, Model.LabelForCurrentAction(), new { @class = "btn btn-primary btn-large", type = "submit" })
MVC5,Bootstrap版本为3.2.0
@Html.ActionLink
(
linkText: " Remove",
actionName: "Index",
routeValues: null, // or to pass some value -> routeValues: new { id = 1 },
htmlAttributes: new { @class = "btn btn-success btn-sm glyphicon glyphicon-remove" }
)
Razor似乎没有“Button”HTML助手。您可能会发现对自定义构建的HTML助手扩展的引用。
请参见此处:http://www.asp.net/mvc/tutorials/creating-custom-html-helpers-cs