Search code examples
razorrazorengine

Mixing RazorEngine Syntax with javascript


On my view page, I want to display some buttons if certain conditions are met, the code goes like this:

<script>
var html = "";
html += "<button>View Member</button>";
@if (Model.HasPrivilage["UPDATE_MEMBER"] == true) {
    html += "<button>Add Member</button>";
    html += "<button>Remove Member</button>";
}
$("#container").html(html);
</script>

However it throws TemplateCompilationException exception, it seems that I have to do something to wrap up javascript texts, how can I improve my code to make it work ?


Solution

  • Yes. The Code inside the if Statement gets Interpreted as C#. You need to say that it is a string by using @: and than write the code.

    So change the if like this

    @if(...){
        @:html += ....
        @:html += ...
    )