这很奇怪。我看到那里有@ Html.Button()的引用,但是当我键入Intellisense时,找不到这样的帮手...有下拉列表,隐藏的,编辑器等等,但没有按钮!
那是怎么回事?
这很奇怪。我看到那里有@ Html.Button()的引用,但是当我键入Intellisense时,找不到这样的帮手...有下拉列表,隐藏的,编辑器等等,但没有按钮!
那是怎么回事?
Answers:
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());
}
}
@Html.Button("ButtonText", new {Name = "name", Value = "value" })
格式,请添加此类:public static MvcHtmlString Button(this HtmlHelper helper, string text, object htmlAttributes) { return Button(helper, text, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes)); }
HtmlHelper.AnonymousObjectToHtmlAttributes
?
从mvc预览版3开始,没有按钮助手(不是mvc3)
例如,过去曾提到过很多:http : //blog.wekeroad.com/blog/aspnet-mvc-preview-using-the-mvc-ui-helpers/ 但是滚动自己是微不足道的-我有一个基础为此,我必须对其进行挖掘,但实际上只是创建一个新的Html.ButtonFor并从Html.LabelFor复制源代码,然后更改输出以创建输入type =“ button”标记。
为了扩展已接受的答案,因此您可以将提交按钮绑定到模型属性,但要使用不同的文本:
@Html.ButtonFor(m => m.Action, Model.LabelForCurrentAction(), new { @class = "btn btn-primary btn-large", type = "submit" })
使用以下稍微修改的Button
帮助程序类:
/// <summary>
/// Via /programming/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版本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似乎没有“按钮” HTML助手。您可能会找到对自定义HTML帮助程序扩展的引用。
参见此处:http : //www.asp.net/mvc/tutorials/creating-custom-html-helpers-cs