How to disable a button more elegantly

前端 未结 8 867
面向向阳花
面向向阳花 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)
    
    0 讨论(0)
  • 2020-12-14 17:40

    I don't know what language you're using, but you might be able to move your if statement closer to the actual different between the two lines:

    <input type="button" class="btnresetinvoice button" value="Reset"
           data-invoiceid="@item.InvoiceId"
           @{ if(item.PMApproved != true) { 
                 @:disabled="disabled" 
            } }
    />
    
    0 讨论(0)
提交回复
热议问题