使用jquery在html表中显示Model(Dictionary)中的数据

时间:2015-07-13 13:49:24

标签: c# jquery asp.net-mvc razor model

我正在使用ASP.Net Razor,ASP.NET MVC和jQuery。

我在Model中有一个字典,在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>

1 个答案:

答案 0 :(得分:0)

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

<script>
    var dicMap = new Map();

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

现在改变:

// this is not working 
$.each(dicMap, function (key, value) {