简体   繁体   English

如何在javascript代码和asp.net razor mvc代码中利用相同的变量?

[英]How do I utilize the same variable in both javascript code and asp.net razor mvc code?

How do I utilize the same variable in both JavaScript code and ASP.NET MVC Razor code? 如何在JavaScript代码和ASP.NET MVC Razor代码中利用相同的变量?

As you can see from the code below, I created a dropdown and filled it with data. 从下面的代码中可以看到,我创建了一个dropdown并在其中填充了数据。

I want the selected item to be utilized in the below div . 我希望在以下div使用所选的项目。

  • When the user clicks on an item, what is contained within that div is revealed 当用户单击某个项目时,该div包含的内容就会显示出来
  • Then the selected string item is used within the if statement within the div 然后在divif statement使用选定的字符串项

What currently happens is, the selected item isn't globally accessible, causing the issues you can see below. 当前发生的情况是,所选项目无法全局访问,从而导致出现以下问题。

@using (Html.BeginForm())
{
    @Html.ValidationSummary(true)
    <div class="display-field" align="left">
        <select name="mydropdown" id="mydropdown" onchange="onChange()">
        @foreach (var lLMMI in lIM)
        {
            <option value="@lLMMI.Key.Product.PCode">
                @locItem.Key.Loc.N (@lLMMI.Key.Loc.Code)
            </option>
        }
        </select>
        <br /><br />
    </div>
}

var itemselected = "";

<div>
    <script>
        function onChange() {
            var item = document.getElementById("mydropdown").value;
            $('#summary').show();
        }
    </script>

    <div id="summary">

        @foreach (var lLMMI in lIM)
        {
            if (@lLMMI.Key.Pro.PCode == itemselected.toString())
            {
                <summary>extra html elements are added etc.</summary>
            }
        }

You can't use the Javascript variable in the Razor code for two reasons: 由于以下两个原因,您无法在Razor代码中使用Javascript变量:

  • They don't exist at the same time 它们不同时存在
  • They don't exist in the same place 它们不在同一个地方

The Razor code runs on the server to generate the page that is sent to the browser. Razor代码在服务器上运行,以生成发送到浏览器的页面。 When the page arrives to the browser it is parsed and the Javascript code runs. 当页面到达浏览器时,将对其进行解析并运行Javascript代码。 To use the Javascript variable in the Razor code would be a time paradox, because it would need to send the value back in time to change how the page was created, but keep the original time line in between... 在Razor代码中使用Javascript变量将是一个时间悖论,因为它需要及时将值发送回以更改页面的创建方式,但要保持原始时间线介于两者之间。

There are basically two different ways of updating a page depending on input from the user: 根据用户的输入,基本上有两种不同的页面更新方式:

  • Reload the page, including the data in the request. 重新加载页面,包括请求中的数据。
  • Use an AJAX call to request that part of the page, including the data as a parameter. 使用AJAX调用来请求页面的该部分,包括数据作为参数。

I can't provide you the actual code right now, but here it goes. 我现在无法为您提供实际的代码,但是可以了。

You have the following options for your dropdown . 您的dropdown有以下选项。

<option value="@lLMMI.Key.Product.PCode">
    @locItem.Key.Loc.N (@lLMMI.Key.Loc.Code)
</option>

For example, you want to show @lLMMI.Key.Product.PCode + @lLMMI.Key.Product.PName + @lLMMI.Key.Product.PAddress . 例如,您要显示@lLMMI.Key.Product.PCode + @lLMMI.Key.Product.PName + @lLMMI.Key.Product.PAddress Why not load what you will show to your options' values and show them when selected? 为什么不将要显示的内容加载到选项的值中,并在选择时显示它们呢? Like: 喜欢:

<option value="@lLMMI.Key.Product.PCode + @lLMMI.Key.Product.PName + @lLMMI.Key.Product.PAddress">
    @locItem.Key.Loc.N (@lLMMI.Key.Loc.Code)
</option>

And change the function to: 并将函数更改为:

function onChange() {
    var myDropDown = document.getElementById("mydropdown");
    var myValue = myDropDown.options[myDropDown.selectedIndex].value;
    var myDiv = document.getElementById("summary");
    fieldNameElement.innerHTML = myValue;
}

As others mentioned, you cannot play with Razor (server side) and Javascript (client side) like that. 正如其他人提到的那样,您不能像这样使用Razor (服务器端)和Javascript (客户端)。 Think of Razor code running only once at the page load. 考虑一下Razor代码在页面加载时仅运行一次。 If you want to do stuff after the page load, you need to assign your Razor variables to some client side elements and operate with client side code, JavaScript in your example. 如果要在页面加载后做一些事情,则需要将Razor变量分配给某些客户端元素,并使用客户端代码(示例中的JavaScript)进行操作。

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

相关问题 如何使用ASP.NET MVC将JavaScript变量传递给ASP代码 - How to pass javascript variable to asp code with ASP.NET MVC 在ASP.NET MVC中,如何将ASP.NET剃须刀字符串传递到javascript onclick函数中? - In ASP.NET MVC how do I pass in ASP.NET razor strings into a javascript onclick function? Javascript变量包含asp.net mvc剃刀代码而不是值 - Javascript variable contains asp.net mvc razor code instead of value 在Asp.net MVC Razor语法中使用JavaScript变量 - Use JavaScript Variable in Asp.net MVC Razor syntax 在 ASP.net MVC Razor 代码中调用按键事件的 javascript 函数 - Calling javascript function on key up event in ASP.net MVC Razor code 如何为HTML助手ASP.NET MVC编写Javascript代码? - How to write Javascript code for HTML helpers ASP.NET MVC? ASP.NET MVC 3 Razor:如何在Javascript字符串变量中获取Action URL? - ASP.NET MVC 3 Razor: How to get Action URL in a Javascript string variable? 我可以将JavaScript变量传递给内联ASP.NET代码吗? - Can I pass a javascript variable to inline ASP.NET code? 如何在asp.net mvc中使用razor viewmodel将c#guid值赋给javascript变量? - How to assign c# guid value to javascript variable using razor viewmodel in asp.net mvc? 是否可以在 ASP.NET Core 的 javascript 代码中使用 razor 语法 - Is it possible to use razor syntax in a javascript code in ASP.NET Core
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM