剃刀的选项选择对我不起作用

本文关键字:不起作用 选择 选项 剃刀 | 更新日期: 2023-09-27 18:10:30

这段代码不适合我:

<div class="col-md-4">
  @Html.DropDownListFor(m => m.etattik, 
    new SelectList(new List<Object>
    { 
      new { value = 0 , text="text1"  },
      new { value = 1 , text = "text2" },
      new { value = 2 , text = "text3"}
    })
  )
  @Html.ValidationMessageFor(m => m.etattik)
</div>

显示的是{ value = 0 , text=text1 }

剃刀的选项选择对我不起作用

您没有为SelectList指定dataValueFielddataTextField:

@Html.DropDownListFor(
                        m => m.etattik, 
                        new SelectList(
                                  new List<Object>{ 
                                   new { value = 0 , text="text1"  },
                                new { value = 1 , text = "text2" },
                                  new { value = 2 , text = "text3"}
                                }
                    , "value", "text")
                        )

检查

@Html.DropDownListFor(m => m.test,
                        new List<SelectListItem>(){
                         new SelectListItem() { Value = "0", Text = "text1" },
                         new SelectListItem() { Value = "1" , Text="text2" },
                         new SelectListItem() { Value = "2" , Text="text3" }
                        }
                        )