在MVC DropDownListFor中插入AngularJs ng-model

时间:2017-01-17 03:48:18

标签: angularjs asp.net-mvc asp.net-mvc-4 dropdownlistfor

我已按照here的说法对下拉列表的值进行了硬编码,但我无法弄清楚如何在<select>内实现AngularJS的 ng-model 在此特定设置中,我可以插入ng-model="categoryValue"并获取下拉列表值。

<div class="editor-field" ng-model="categoryValue">
            @Html.DropDownListFor(model => model.ticketType, 
            new SelectList(
              new List<Object>{
                   new { value = "Hardware/Devices" , text = "Hardware/Devices" },
                   new { value = "Software/Drivers" , text = "Software/Drivers" },
                   new { value = "Access Tools" , text = "Access Tools"}
                },
              "value",
              "text",
               1))
            @Html.ValidationMessageFor(model => model.ticketType)
        </div>

上面的代码转换为

<select id="ticketType" name="ticketType">
<option value="Hardware/Devices">Hardware/Devices</option>
<option value="Software/Drivers">Software/Drivers</option>
<option value="Access Tools">Access Tools</option>
</select>

1 个答案:

答案 0 :(得分:0)

从div中删除ng-model并在下拉列表中应用,如下所示,希望它能正常工作

<div class="editor-field">
            @Html.DropDownListFor(model => model.ticketType, 
            new SelectList(
              new List<Object>{
                   new { value = "Hardware/Devices" , text = "Hardware/Devices" },
                   new { value = "Software/Drivers" , text = "Software/Drivers" },
                   new { value = "Access Tools" , text = "Access Tools"}
                },
              "value",
              "text",
               1),new
                   {
                       ng_model = "categoryValue",
                   })
            @Html.ValidationMessageFor(model => model.ticketType)
        </div>