how do I set disabled attribute on html textbox in asp.net-mvc?

前端 未结 3 659
走了就别回头了
走了就别回头了 2021-01-05 05:12

I am trying to dynamically set the disabled attribute on the html textbox and having issues

I tried this in my view:

 string disabledString = \"\";
          


        
3条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-05 05:44

    This was ugly for us, due to the fact that the HTML spec is lousy here.

    Basically in our view code we had some logic like this:

    bool isPlatformOwner = false;
    
    object disabledAttributes = new { @disabled="disabled", @readonly="readonly" };
    
    //omitted code setting isPlatformOwner    
    
        if (isPlatformOwner)
        {
            disabledAttributes = new { };
        }
    

    Then, for our controls, we had this:

    <%=Html.CheckBoxFor(f => f.AddToReleaseIndicator, disabledAttributes)%>
    

    Anonymous types saved us here, but, like I said, it got a little ugly.

提交回复
热议问题