[Vuejs]-How do I set Razor variabele in Javascript?

3👍

Unfortunately, you cannot get JavaScript to change a value of your backend C# code because JavaScript, once rendered on the frontend, does not know anything about your C# code on the server.

However, you can assign a C# value to a JavaScript variable before it leaves the server. What this is doing is just injecting the value of @filterData into the html page in the script and then is assigned to the real JavaScript variable in the actual JS environment during runtime:

<script>
    var x;
    var filterData = @filterData;
    var otherData = @otherData;
    var newData = @newData;

    function loadShipmentsByCondition() {
        x = document.getElementById("conditionSelect").value;
        if (x === "CMR not received") {
            filterData = newData;
        } else if(x === "Not invoiced") {
            console.log("INVOICE");
            filterData = otherData;
        }
    }
</script>

So you are essentially converting those C# variables into JavaScript variables, but if you really need to change the C# variables then you would have to do a postback, or AJAX call to the server.

👤Mayron

Leave a comment