简体   繁体   English

如何在Javascript中设置Razor variabele?

[英]How do I set Razor variabele in Javascript?

I'm setting Razor variables with data from my controller like this: 我正在使用来自控制器的数据设置Razor变量,如下所示:

@{
    var filterData = ((ShipmentController)this.ViewContext.Controller).GetNoCmrs(new List<int>() { 1 });
}

I want to change this data based on the selected option the user selects (within a select). 我想根据用户选择的选项(在选择范围内)更改此数据。

<script>
    var x;
    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>

Is this possible? 这可能吗? Or if not, how can I do this efficiently? 否则,我该如何有效地做到这一点? I'm using MVC ASP.NET and vue js as front-end. 我正在使用MVC ASP.NET和vue js作为前端。

Thanks 谢谢

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. 不幸的是,您无法使JavaScript更改后端C#代码的值,因为JavaScript一旦在前端呈现,就对服务器上的C#代码一无所知。

However, you can assign a C# value to a JavaScript variable before it leaves the server. 但是,您可以在JavaScript变量离开服务器之前为其分配C#值。 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: 这样做只是将@filterData的值注入脚本中的html页面,然后在运行时在实际的JS环境中将其分配给实际的JavaScript变量:

<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. 因此,您实际上是将这些C#变量转换为JavaScript变量,但是如果您确实需要更改C#变量,则必须对服务器执行回发或AJAX调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在cshtml(razor)上设置属性? - How do I set attribute on cshtml (razor)? 如何设置带有“日期”字段的Razor“ EditorFor”的“ onchange”事件? - How do I set an “onchange” event of a Razor “EditorFor” with a Date field? 如何从剃刀部分引用JavaScript变量? - How do I reference a javascript variable from a razor section? 如何强制Razor在JavaScript中检查TextBox上的验证 - How do I force Razor to check validation on TextBox in JavaScript 如何在razor foreach中使用html和escape razor? - How do I use html and escape razor inside a razor foreach? 如何在ASP.NET MVC4(剃刀)中设置TinyMCE编辑器的初始内容? - How do I set the initial content of a TinyMCE editor in ASP.NET MVC4 (Razor)? 如何为授权挑战设置两个不同的登录路径? (ASP.NET Core Razor 页面) - How do I set two different login paths for authorization challenge? (ASP.NET Core Razor Pages) 如何在javascript代码和asp.net razor mvc代码中利用相同的变量? - How do I utilize the same variable in both javascript code and asp.net razor mvc code? 如何使用对象MVC 5 C#的Javascript列表填充剃须刀DropDownListFor - How do I fill razor DropDownListFor with Javascript list of object MVC 5 C# 如何摆脱使用Razor填充的Javascript数组末尾的逗号? - How do I get rid of the comma at the end of this Javascript array that is populated using Razor?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM