简体   繁体   中英

Drop down menu in ASP.net is not working(MVC)

 <div class="form-group">
   <div class="col-md-10"
     @Html.LabelFor(expression: x => Model.MOCType, htmlAttributes: new { @class = "control-label col-md-2" })
     @Html.DropDownListFor(expression: x => Model.MOCType, selectList: new SelectList("CPPSMOC", "SPRDMOC"), optionLabel: "", htmlAttributes: new { @class = "text-danger" })

  </div>
 </div>

Drop down menu code is given above in MVC method of ASP.net but results are not as expected

If you have a static list, try with below code.

<div class="form-group">
   <div class="col-md-10">
     @Html.LabelFor(x => Model.MOCType, new { @class = "control-label col-md-2" })   
     @Html.DropDownListFor(x => Model.MOCType, new SelectList(
                                              new List<Object>{ 
                                                   new { value = 0 , text = "CPPSMOC"  },
                                                   new { value = 1 , text = "SPRDMOC" }
                                                },"value","text",2),
                                               new { @class = "text-danger" })

  </div>
 </div>

If you have dynamic list then try with below code.

<div class="form-group">
   <div class="col-md-10">
    @Html.LabelFor(x => Model.MOCType, new { @class = "control-label col-md-2" })   
    @Html.DropDownListFor(model => model.MOCType, new SelectList(
              Model.DropdownSelectList, "id", "name", 1), new { @class = "text-danger" })
  </div>
</div>

SelectList的第一个参数必须是IEnumerable,请将自定义列表(IEnumrable)传递给SelectList。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM