How to use if else statement in cshtml based on id?

Viewed 42

I was trying to do an if else statement on my cshtml page. If the select value was given then the next select options should change. But how do I implement it without using Javascript or JQuery?

<select class="form-control" id="SomethingHere" name="SomethingHere" onchange="show(options[this.selectedIndex].value);">
   <option value="SomethingHere">SomethingHere</option>
</select>

This is my first select option when I choose something on my first select, then on my second select option would change the option list

<select class="form-control" id="SomethingHere2" name="SomethingHere2" onchange="show2(options[this.selectedIndex].value);">
   @ {
     var SomethingHere = document.getElementById('SomethingHere').val();
     //This is somehow I tried so far.
     @if(SomethingHere ==  "SomethingHere") {
        <option value="SomethingHere2">SomethingHere2</option>
     }
   }
</select>

Is there any other way to achieve this scenario?

1 Answers

It's not possible without JavaScript because the razor syntax only works in server-side rendering. for any update/modification you need after that, you must use JavaScript.

If you want to solve this problem without JavaScript then you should switch your front end to Blazor instead of Asp.net Core

Related