这很奇怪。我看到了@Html.Button()的引用,但当我输入智能感知没有找到这样的助手时……有下拉列表,隐藏,编辑器,等等,但是没有按钮!
这是怎么回事?
发布于 2011-05-11 05:08:45
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());
}
}
发布于 2011-05-11 04:16:24
在mvc预览版3中没有按钮助手(不是mvc3)
在过去的很多例子中都提到过:http://blog.wekeroad.com/blog/aspnet-mvc-preview-using-the-mvc-ui-helpers/然而滚动你自己的按钮是微不足道的-我在这里有它的基础我必须把它挖出来,但本质上只是创建一个新的按钮并从Html.LabelFor复制源代码并改变输出来创建一个输入按钮“type=”标签。
发布于 2014-04-03 04:37:45
在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);
}
}
https://stackoverflow.com/questions/5955571
复制相似问题