Selected 属性 中的 SelectListItem 不起作用
SelectListItem in Selected Property doesnt work
控制器
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
我现在怎么办?
@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>
}
}
控制器
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 我现在怎么办?
@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>
}
}