.net 没有 @Html.Button !
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5955571/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
There's no @Html.Button !
提问by ekkis
this is weird. I see references out there for @Html.Button() but when I type that Intellisense doesn't find such a helper... there's dropdownlist, hidden, editors, et cetera, but no button!
这很奇怪。我看到@Html.Button() 的引用,但是当我输入 Intellisense 没有找到这样的帮助程序时......有下拉列表、隐藏、编辑器等等,但没有按钮!
what's up with that?
那是怎么回事?
采纳答案by jessegavin
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());
}
}
回答by Adam Tuliper - MSFT
There is no button helper as of mvc preview 3 (not mvc3)
从 mvc preview 3(不是 mvc3)开始,没有按钮助手
it was mentioned a bunch in the past for example: http://blog.wekeroad.com/blog/aspnet-mvc-preview-using-the-mvc-ui-helpers/however rolling your own is trivial - I have a basis for it somewhere around here I'll have to dig it up, but essentially just create a new Html.ButtonFor and copy the source code from Html.LabelFor and change the output to create an input type="button" tag.
过去有人提到过它,例如:http: //blog.wekeroad.com/blog/aspnet-mvc-preview-using-the-mvc-ui-helpers/但是滚动你自己的东西是微不足道的 - 我有一个基础对于它附近的某个地方,我必须将其挖掘出来,但基本上只需创建一个新的 Html.ButtonFor 并从 Html.LabelFor 复制源代码并更改输出以创建一个 input type="button" 标记。
回答by drzaus
To expand on the accepted answer, so you can bind a submit button to a model property but have different text:
要扩展已接受的答案,您可以将提交按钮绑定到模型属性但具有不同的文本:
@Html.ButtonFor(m => m.Action, Model.LabelForCurrentAction(), new { @class = "btn btn-primary btn-large", type = "submit" })
Using the following slightly modified Buttonhelper class:
使用以下稍微修改的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);
}
}
回答by Piotr Knut
MVC5 , Bootstrap ver 3.2.0
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" }
)
This will generate a link that looks like a button.
这将生成一个看起来像按钮的链接。
回答by Ben Finkel
Razor does not appear to have a "Button" HTML helper. You're likely finding references to a custom-built HTML helper extension.
Razor 似乎没有“按钮”HTML 帮助程序。您可能会找到对自定义构建的 HTML 帮助程序扩展的引用。
See here: http://www.asp.net/mvc/tutorials/creating-custom-html-helpers-cs
见这里:http: //www.asp.net/mvc/tutorials/creating-custom-html-helpers-cs

