How to disable a button more elegantly

前端 未结 8 866
面向向阳花
面向向阳花 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:16
    <button @(isEnabled ? null : "disabled")>Butt</button>
    
    0 讨论(0)
  • 2020-12-14 17:17

    Using asp.net mvc5 razor:

    @if(condition)
    {
       <button data-toggle="collapse" data-target="#content">Details</button>
    }
    else
    {
       <button disabled>Details</button>
    }
    

    It prevents attempting to enable button from DevTools, because razor not visible for DevTools

    0 讨论(0)
  • 2020-12-14 17:20

    <input type="button" value="Reset" @{@((!item.PMApproved) ? null : new { disabled = "disabled" })}; />

    No need for that bloated code, just keep it simple :-)

    0 讨论(0)
  • 2020-12-14 17:26

    A markup-centric solution aided by a new extension method:

    public static class HtmlExtensions
    {
       public static HtmlString DisabledIf(this HtmlHelper html, bool condition)
       {
          return new HtmlString(condition ? "disabled=\"disabled\"" : "");
       }
    }
    

    In your views, reuse out the wazoo:

    <button type="reset" @Html.DisabledIf(someCondition)>Clear Fields</button>
    

    Nicely reusable, and the rendered markup is very clean with regards to whitespace:

    <button type="reset" disabled="disabled">Clear Fields</button>
    
    0 讨论(0)
  • 2020-12-14 17:26

    try this

    <button type="submit" disabled="@(!item.PMApproved)"></button>
    
    0 讨论(0)
  • 2020-12-14 17:31

    Possible easy way:

    <input type="button" @(item.PMApproved ? "disabled" : "") />
    
    0 讨论(0)
提交回复
热议问题