在dropdownlist mvc中未定义selectedIndex

时间:2015-06-10 05:21:51

标签: c# asp.net-mvc angularjs

我正在绑定下拉列表,但零索引显示未定义,下面是我的代码。

  <select ng-model="CategoryID" id="CategoryID" name="CategoryID" class="form-control margin-bottom-15" required>          
        @foreach (var item in Model.Categories)
        {
            <option value="@item.Value">@item.Text</option>
        }
    </select>

渲染后显示为......

<select ng-model="CategoryID" id="CategoryID" name="CategoryID" class="form-control margin-bottom-15 ng-pristine ng-invalid ng-invalid-required ng-touched" required="">
   <option value="? undefined:undefined ?"></option>                          
   <option value="1">Category1</option>
   <option value="2">Category2</option>
</select>
控制器中的

..

 private IEnumerable<SelectListItem> GetCategories()
    {

        var catg = database.Categories
                    .Select(x =>
                            new SelectListItem
                            {
                                Value = x.CategoryID.ToString(),
                                Text = x.CategoryName
                            });

        return new SelectList(catg, "Value", "Text");
    }

1 个答案:

答案 0 :(得分:1)

检查绑定的值,因为它们可能如下所示:

let myClassRef = AClass.self // store a reference to the type of AClass

if myObject.isKindOfClass(myClassRef) {
// Do something
}

或者您确实将此值[ { Text: "" }, { Text: "Category1", Value: 1 } { Text: "Category2", Value: 2 } ] 作为第一个项目的? undefined:undefined ?属性。