[英]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
使用所选的项目。
div
is revealed div
包含的内容就会显示出来 if statement
within the div
div
的if 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变量:
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: 根据用户的输入,基本上有两种不同的页面更新方式:
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.