2
我想填充數值的下拉,帶C.The下面的代碼工作只有創造,而不是編輯。如何綁定匿名類下拉值
ViewBag.UserType = new[] { new SelectListItem { Text = "A", Value = "1", Selected = false }, new SelectListItem { Text = "B", Value = "2", Selected = false }, new SelectListItem { Text = "C", Value = "3", Selected = false } };
所以我創建了一個匿名類。我有一個像下面這樣的匿名課程。
var Roles= new[]
{ new { Id =1,UserType="A" },
new {Id=2,UserType="B" },
new {Id=3,UserType="B" }
};
ViewBag.Type = Roles.ToList();
但我不知道如何在視圖中填充下拉菜單。
@Html.DropDownListFor(model => model.UserType, ((IEnumerable<SelectListItem>)ViewBag.UserType).Select(option => new SelectListItem
{
Text = (option == null ? "None" : option.Text),
Value = option.Value.ToString(),
Selected = (Model != null) && (option.Value == (Model.UserType).ToString())
}),"Select")
,我應該怎樣更改視圖
什麼你的模型的類型,你發送給視圖的那個? –