How to add a second css class with a conditional value in razor MVC 4

后端 未结 4 842
傲寒
傲寒 2021-01-30 02:32

While Microsoft has created some automagic rendering of html attributes in razor MVC4, it took me quite some time to find out how to render a second css class on an element, bas

4条回答
  •  盖世英雄少女心
    2021-01-30 03:26

    You can add property to your model as follows:

        public string DetailsClass { get { return Details.Count > 0 ? "show" : "hide" } }
    

    and then your view will be simpler and will contain no logic at all:

        

    This will work even with many classes and will not render class if it is null:

        

    with 2 not null properties will render:

        

    if class1 is null

        

提交回复
热议问题