0

At times I need to have the css class for div to have values, at other times I don't want to have class attribute or it's properties

<div> 
<div class="highligh"> 

I have tried using a function as such

<div @GetCssClass(Model.IsThisPropertyHighlighted)>@Model.ThisProperty

but the problem is I can either get

<div > or <div class="">

Neither of them is satisfactory,

of course I also get <div class="highligh"> which is the only satisfactory case.

jimjim
  • 2,414
  • 2
  • 26
  • 46
  • 1
    You can always write your own extension methods to generate the html (for example something like [this answer](http://stackoverflow.com/questions/26519493/customattribute-reflects-html-attribute-mvc5/26519946#26519946)) where you might have a `[ClassName("highlight")]` attribute –  Jul 13 '16 at 03:45

1 Answers1

1

Please avoid calling functions in razor view.

Instead create a property in the model and in your razor view you can simply specify.

<div class="@Model.NewProperty">

If NewProperty is null class attribute will not be rendered at all.

Anonymous Duck
  • 2,942
  • 1
  • 12
  • 35