我该如何利用这两个JavaScript的code和asp.net mvc的剃须刀code相同的变量? [英] How do I utilize the same variable in both javascript code and asp.net razor mvc code?

查看:101
本文介绍了我该如何利用这两个JavaScript的code和asp.net mvc的剃须刀code相同的变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要如何利用这两个的JavaScript code和相同的变量 ASP.NET MVC剃刀 code?

你可以从下面的code看到的,我创建了一个下拉菜单并用数据填充它。

我想在要利用以下 DIV 选定的项目。


  • 当用户点击一个项目,什么是包含内 DIV 揭晓

  • 然后选择字符串项中使用的 if语句 DIV

目前是什么情况是,所选择的项目是不是全局访问,从而导致你可以看到下面的问题。

  @using(Html.BeginForm())
{
    @ Html.ValidationSummary(真)
    < D​​IV CLASS =显示字段align =left>
        <选择名称=mydropdownID =mydropdown的onchange =的onChange()>
        @foreach(VAR lLMMI在LIM)
        {
            <期权价值=@ lLMMI.Key.Product.P code>
                @ locItem.Key.Loc.N(@ lLMMI.Key.Loc。code)
            < /选项>
        }
        < /选择>
        < BR />< BR />
    < / DIV>
}变种itemselected =;< D​​IV>
    <脚本>
        起作用的onChange(){
            VAR项目=的document.getElementById(mydropdown)值。
            $('#摘要)显示()。
        }
    < / SCRIPT>    < D​​IV ID =摘要>        @foreach(VAR lLMMI在LIM)
        {
            如果(@ lLMMI.Key.Pro.P code == itemselected.toString())
            {
                <总结>额外的HTML元素添加等< /总结>
            }
        }


解决方案

我无法为您提供实际的code的权利,但在这里不言而喻。

您为您的下拉菜单下面的选项

 <期权价值=@ lLMMI.Key.Product.P code>
    @ locItem.Key.Loc.N(@ lLMMI.Key.Loc。code)
< /选项>

例如,要显示 @ lLMMI.Key.Product.P code + @ + lLMMI.Key.Product.PName @ lLMMI.Key.Product.PAddress 。为什么不加载,你会展示给你的选择的价值观是什么,并告诉他们选择什么时候?这样的:

 <期权价值=@ lLMMI.Key.Product.P code + @ + lLMMI.Key.Product.PName @ lLMMI.Key.Product.PAddress&GT ;
    @ locItem.Key.Loc.N(@ lLMMI.Key.Loc。code)
< /选项>

和改变功能:

 函数的onChange(){
    变种myDropDown =的document.getElementById(mydropdown);
    VAR myvalue的= myDropDown.options [myDropDown.selectedIndex] .value的;
    VAR myDiv =的document.getElementById(摘要);
    fieldNameElement.innerHTML = myvalue的;
}

正如其他人所提到的,你不能用剃须刀类似的(服务器端)和的JavaScript (客户端)玩。想想剃须刀 code。在页面加载运行只有一次的。如果你想在页面加载后做的东西,你需要指定您的剃须刀变量的一些客户端的元素,并与客户端code操作,的JavaScript 在您的例子。

How do I utilize the same variable in both JavaScript code and ASP.NET MVC Razor code?

As you can see from the code below, I created a dropdown and filled it with data.

I want the selected item to be utilized in the below div.

  • When the user clicks on an item, what is contained within that div is revealed
  • Then the selected string item is used within the if statement within the div

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>
            }
        }

解决方案

I can't provide you the actual code right now, but here it goes.

You have the following options for your 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. 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. Think of Razor code running only once at the page load. 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.

这篇关于我该如何利用这两个JavaScript的code和asp.net mvc的剃须刀code相同的变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆