首页 > 解决方案 > 选定属性中的 SelectListItem 不起作用

问题描述

控制器

Kullanicilar kullanici = db.Kullanicilar.Where(f => f.Id == 
   userProfileModel.UserModel.Kullanici.UserID).FirstOrDefault();

foreach (var item in db.Tanimlar.Where(f => f.TanimTipId == (int)ETanimTip.Kategoriler))
userProfileModel.AvaliableCategories.Add(item: new SelectListItem() 
{ 
    Text = item.Kod, 
    Value = item.Id.ToString(), 
    Selected = item.Id == kullanici.CategoryId 
});

看法

@Html.DropDownListFor(f => f.CategoryId, Model.AvaliableCategories, 
  htmlAttributes: new { @class = "form-control", id = "AvaliableCategories"  })

当我查看选定的值时,我看到 true 但查看结果为 false .. true value false result 我现在该怎么办?

标签: asp.net-mvc-4

解决方案


@foreach (var item in Model.AvaliableCategories)
                {
                    if (item.Selected)
                    {
                        <option value="@item.Value" selected="selected">@item.Text</option>
                    }
                    else
                    {
                        <option value="@item.Value">@item.Text</option>
                    }
                }

推荐阅读