没有@ Html.Button!

这很奇怪。 我看到@ Html.Button()的引用,但是当我inputIntellisense没有find这样一个帮手…有下拉列表,隐藏,编辑等等,但没有button!

那是怎么回事?

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()); } } 

从mvc预览3(不是mvc3)没有button助手

有人在过去曾经提到过,例如: http : //blog.wekeroad.com/blog/aspnet-mvc-preview-using-the-mvc-ui-helpers/但是你自己的做法是微不足道的 – 我有一个基础因为它在这里的某个地方我必须挖掘它,但基本上只是创build一个新的Html.ButtonFor并从Html.LabelFor复制源代码,并更改输出以创build一个inputtype =“button”标记。

为了扩大接受的答案 ,所以你可以绑定一个提交button到一个模型属性,但有不同的文本:

 @Html.ButtonFor(m => m.Action, Model.LabelForCurrentAction(), new { @class = "btn btn-primary btn-large", type = "submit" }) 

使用以下稍微修改的Button助手类 :

 /// <summary> /// Via https://stackoverflow.com/questions/5955571/theres-no-html-button /// </summary> public static class HtmlButtonExtension { public static MvcHtmlString Button(this HtmlHelper helper, object innerHtml, object htmlAttributes) { return helper.Button(innerHtml, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes)); } public static MvcHtmlString Button(this HtmlHelper helper, object innerHtml, IDictionary<string, object> htmlAttributes) { var builder = new TagBuilder("button") { InnerHtml = innerHtml.ToString() }; builder.MergeAttributes(htmlAttributes); return MvcHtmlString.Create(builder.ToString()); } public static MvcHtmlString ButtonFor<TModel, TField>(this HtmlHelper<TModel> html, Expression<Func<TModel, TField>> property, object innerHtml, object htmlAttributes) { // lazily based on TextAreaFor var attrs = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes); var name = ExpressionHelper.GetExpressionText(property); var metadata = ModelMetadata.FromLambdaExpression(property, html.ViewData); string fullName = html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(name); ModelState modelState; if (html.ViewData.ModelState.TryGetValue(fullName, out modelState) && modelState.Errors.Count > 0) { if( !attrs.ContainsKey("class") ) attrs["class"] = string.Empty; attrs["class"] += " " + HtmlHelper.ValidationInputCssClassName; attrs["class"] = attrs["class"].ToString().Trim(); } var validation = html.GetUnobtrusiveValidationAttributes(name, metadata); if(null != validation) foreach(var v in validation) attrs.Add(v.Key, v.Value); string value; if (modelState != null && modelState.Value != null) { value = modelState.Value.AttemptedValue; } else if (metadata.Model != null) { value = metadata.Model.ToString(); } else { value = String.Empty; } attrs["name"] = name; attrs["Value"] = value; return html.Button(innerHtml, attrs); } } 

MVC5,Bootstrap ver 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" } ) 

这将生成一个看起来像一个button的链接。

剃刀似乎没有“button”的HTML帮手。 您可能会find对定制的HTML帮助器扩展的引用。

看到这里: http : //www.asp.net/mvc/tutorials/creating-custom-html-helpers-cs