Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/258.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用jquery在html表中显示模型(字典)中的数据_C#_Jquery_Asp.net Mvc_Razor_Model - Fatal编程技术网

C# 使用jquery在html表中显示模型(字典)中的数据

C# 使用jquery在html表中显示模型(字典)中的数据,c#,jquery,asp.net-mvc,razor,model,C#,Jquery,Asp.net Mvc,Razor,Model,我正在使用ASP.NETRazor、ASP.NETMVC和jQuery 我在模型中有一个字典,在html表中有一个下拉列表。我想要的是,当我在下拉列表中选择ALM选项时,我想使用jquery在id=leftValues中显示字典中的一些数据 我的代码: @{ Layout = "~/Views/Shared/_Layout.cshtml"; } @model Dictionary<string,string> <style type="text/css">

我正在使用ASP.NETRazor、ASP.NETMVC和jQuery

我在模型中有一个字典,在html表中有一个下拉列表。我想要的是,当我在下拉列表中选择ALM选项时,我想使用jquery在id=leftValues中显示字典中的一些数据

我的代码:

@{
    Layout = "~/Views/Shared/_Layout.cshtml";
}


@model Dictionary<string,string>

<style type="text/css">

  #myTable {
    position:absolute;
           top:10%;
           left:30%; 
  }
    #heading {
        text-align: center
    }
    #SelectPlatform {
        text-align: center;
        position: absolute;
            top: 10%;

         }
</style>



<table id="myTable">

        <tr> 
            <th id="heading">  Select Platform   </th>
            <th id="heading"> Available Options</th>
            <th></th>
            <th id="heading"> Selected Options</th>

        </tr>

        <tr>
            <td>
                    <select id="SelectPlatform">
                        <option>Select Platform</option>                      
                        <option>ALM</option>
                        <option>Serena</option>
                    </select>
            </td>
            <td>
                   <div>
                      <select style="text-align: center; text-wrap:normal; width: 110%"; size="20%" id="leftValues" multiple >

                       // Here I want to show data from dictionary 

                      </select>
                    </div>
            </td>

            <td>
                <div>
                    <input style="width: 100%" type="button" id="btnLeft" value="&lt;&lt;"  />
                    <input style="width: 100%" type="button" id="btnRight" value="&gt;&gt;" />
                </div>
            </td>

            <td>
                <div>

                     <select style="text-align: center; text-wrap:normal; width: 110%" id="rightValues" size="20%" multiple >

                    </select>

                </div>

            </td>


        </tr>

</table>



<script type="text/javascript">

    $(document).ready(function () {


        $("#SelectPlatform").change(function () {

            var textValue = $("#SelectPlatform option:selected").text();
            if (textValue == "Select Platform")
            {
                     // some code
            }
            else if (textValue == "ALM")
            {

              // this is working 

                $("#leftValues").append('<option  value=' + '"NoItem"' + ' >HI I am added </option>');   


                // this is not working 
                $.each(Model, function (key, value) {
                    alert(key);
                    alert(value);
                    $("#leftValues").append('<option  value=' + '"NoItem"' + ' >HI I am also added </option>');
                });


            }
            else if (textValue == "Serena")
            {
               // some code
            }


        });




      }

    });

</script>

因此,需要将模型值从前端移动到后端。Javascript无法读取在服务器上处理的ViewModel值。您可以将它们写入javascript:

<script>
    var dicMap = new Map();

    @foreach (var kv in Model)
    {
        @:dicMap.set(@kv.Key, @kv.Value);
    }
</script>

它显示foreach循环中逗号处的语法错误。顺便说一下,我从你的回答中得到了逻辑。我在if conditon中写了这样的东西,它成功了@模型@中的foreachvar项:$leftValues.append+'@item.Value'+;我应该说,代码没有经过测试。如果我的回答有帮助,你能接受吗?这样我就可以得到帮助的分数了?!
// this is not working 
$.each(dicMap, function (key, value) {