我有IEnumerable<SelectListItem>
并且我正在尝试显示其中一个项目中的文本,但是,只显示相关字段中的第一个单词:输出显示为“4th”而不是“4th” “。
这就是我填充ViewBag
:
public IEnumerable<SelectListItem> GetGradeLevelItems(int selectedGradeLevelId = 0)
{
var allGradeLevels = _courseRepository.GetAllGradeLevels();
var gradeLevelItems = new List<SelectListItem>();
if (selectedGradeLevelId == 0) //if 0, we're creating a new course. Let's add a default option to the list
{
gradeLevelItems.Add(new SelectListItem() { Value = "0", Text = "--Choose--", Selected = true });
}
else if (allGradeLevels != null && allGradeLevels.All(gl => gl.GradeLevelId != selectedGradeLevelId)) //if the selected grade level is not in the table
{
selectedGradeLevelId = allGradeLevels?.Min(gl => gl.GradeLevelId) ?? 0;
}
allGradeLevels?.ForEach(gl => gradeLevelItems.Add(new SelectListItem(){
Text = gl.GradeLevelName,
Value = gl.GradeLevelId.ToString(),
Selected = (gl.GradeLevelId == selectedGradeLevelId)
}));
return gradeLevelItems;
}
这是我目前的代码:
<div class="form-group">
@Html.LabelFor(m => m.GradeLevelId, new {@class = "row control-label details-label"})
<div class="row">
<input class="form-control" type="text" disabled value=@ViewBag.GradeList[(int)(@Model.GradeLevelId ?? 0)].Text />
</div>
</div>
任何人都能看到我在哪里错了?在此先感谢您的帮助!
答案 0 :(得分:1)
您需要在属性值
周围添加引号<input class="form-control" type="text" disabled value="@ViewBag.GradeList[(int)(@Model.GradeLevelId ?? 0)].Text" />