这是怎么回事?
答案 0 :(得分:66)
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());
}
}
答案 1 :(得分:8)
mvc preview 3(不是mvc3)
没有按钮助手过去曾提到过一堆例如: http://blog.wekeroad.com/blog/aspnet-mvc-preview-using-the-mvc-ui-helpers/ 然而滚动你自己是微不足道的 - 我有一个基础在这里的某处我将不得不挖掘它,但基本上只是创建一个新的Html.ButtonFor并从Html.LabelFor复制源代码并更改输出以创建一个input type =“button”标签。
答案 2 :(得分:7)
要展开accepted answer,您可以将提交按钮绑定到模型属性但具有不同的文本:
@Html.ButtonFor(m => m.Action, Model.LabelForCurrentAction(), new { @class = "btn btn-primary btn-large", type = "submit" })
使用以下slightly modified Button
helper class:
/// <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);
}
}
答案 3 :(得分:5)
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" }
)
这将生成一个看起来像按钮的链接。
答案 4 :(得分:1)
Razor似乎没有“Button”HTML帮助器。您可能会找到对自定义HTML帮助程序扩展的引用。
见这里:http://www.asp.net/mvc/tutorials/creating-custom-html-helpers-cs