How to disable a button more elegantly

前端 未结 8 876
面向向阳花
面向向阳花 2020-12-14 16:46

I have on one of my views the following razor code:

@if (item.PMApproved != true) {
                    

        
8条回答
  •  青春惊慌失措
    2020-12-14 17:36

    A helper could help:

    public static class HtmlExtensions
    {
        public static IHtmlString ApproveButton(this HtmlHelper htmlHelper, MyViewModel item)
        {
            var button = new TagBuilder("input");
            button.Attributes["type"] = "button";
            button.Attributes["value"] = "Reset";
            button.AddCssClass("btnresetinvoice");
            button.AddCssClass("button");
            button.Attributes["data-invoiceid"] = item.InvoiceId.ToString();
            if (item.PMApproved)
            {
                button.Attributes["disabled"] = "disabled";
            }
            return new HtmlString(button.ToString(TagRenderMode.SelfClosing));
        }
    }
    

    and then:

    @Html.ApproveButton(item)
    

提交回复
热议问题