asp.net-mvc-4 - 选定属性中的 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 我现在该怎么办?
解决方案
@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>
}
}
推荐阅读
- vue.js - 向 vue-chart-js 添加一个 chart-js 插件
- visual-studio - 如何修复 Visual Studio 中的设计视图错误?
- python - 从用户输入添加两个数学表达式
- unity3d - 自 Unity 5 起不再支持如何处理具有非运动刚体的非凸 MeshCollider?
- python - NoReverseMatch at /notes/create/ Reverse for 'notes_list' 未找到任何参数。尝试了 1 种模式:['notes/list/(?P
[^/]+)$'] - mysql - SQL 查询 - 查找最大的数字
- c++ - 为什么 glUseProgram 用 glUniform 调用每一帧?
- r - 我的 Rmd 文件不断收到错误消息,提示“您正在将函数作为全局数据传递”。
- docker - NiFi:为什么我的用户权限不足?
- r - dplyr:如何使用 count() 将列保留在小标题中