[.net] @ Html.Button이 없습니다!

이것은 이상합니다. @ Html.Button ()에 대한 참조가 있지만 Intellisense가 그러한 도우미를 찾지 못한다고 입력하면 … 드롭 다운 목록, 숨김, 편집기 등이 있지만 버튼은 없습니다!

무슨 일이야?



답변

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 아님)부터는 버튼 도우미가 없습니다.

예를 들어
http://blog.wekeroad.com/blog/aspnet-mvc-preview-using-the-mvc-ui-helpers/
그러나 자신의 롤링은 사소한 것입니다. 여기 어딘가에 그것을 파헤쳐 야하지만, 본질적으로 새로운 Html.ButtonFor를 생성하고 Html.LabelFor에서 소스 코드를 복사하고 출력을 변경하여 input 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, 부트 스트랩 버전 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


답변