Search code examples
asp.net-corerazor

How can i set the text for a label in razor page using "if" condition?


I have the following

<label for="source" class="col-md-2 control-label">SourceFolder</label>

I have a value in the javascript , filed is called eventtype. I am trying to do the following

<label for="source" class="col-md-2 control-label">eventtype == 2 ? SourceDirectory 
 :SourceFolder</label>

How can i do this using ternary operator ?


Solution

  • For eventtype is a js variable,you can only use if in js.Here is a demo:

    <label id="myLable" for="source" class="col-md-2 control-label">SourceFolder</label>
    

    js:

    <script>
    var eventtype=2;
    $(function() {
                var data = eventtype == 2 ? "SourceDirectory" : "SourceFolder";
                document.getElementById("myLable").innerHTML = data;
            })
    </script>