How to set disabled in MVC htmlAttribute

后端 未结 8 2344
刺人心
刺人心 2020-12-30 02:06

When using an HTML Helper, what is the best method to set an attribute based on a condition. For example

<%if (Page.User.IsInRole(\"administrator\")) {%&g         


        
8条回答
  •  旧时难觅i
    2020-12-30 02:22

    You'll need to pass a Dictionary, and add the disabled key inside an if statement.

    I recommend making an overload of the extension method that takes a bool disabled parameter and adds it to a RouteValueDictionary created from the attributes parameter if it's true. (You could also remove the disabled entry from the RouteValueDictionary if it's false, and not take another parameter)

提交回复
热议问题