我是Razor的新手,我不知道为什么我选择的值不起作用。
var listItems = (List<Categories>)ViewBag.Categories;
用于
@for (var i = 0; i < Model.TagActivities.Count; i++)
{
<tr>
@Html.HiddenFor(modelItem => Model.TagActivities[i].TAGActivityId, new { htmlAttributes = new { @class = "form-control" } })
@Html.HiddenFor(modelItem => Model.TagActivities[i].CategoryId, new { htmlAttributes = new { @class = "form-control"
<td>
@Html.EditorFor(modelItem => Model.TagActivities[i].ActivityName, new { htmlAttributes = new { @class = "form-control" } })
</td>
<td>
@Html.EditorFor(modelItem => Model.TagActivities[i].ActivityDescription, new { htmlAttributes = new { @class = "form-control" } })
</td>
<td>
@Html.DropDownListFor(modelItem => Model.TagActivities[i].Category.CategoryName, new SelectList(listItems, "CategoryId", "CategoryName", Model.TagActivities[i].Category.CategoryName), new { @class = "form-control" })
</td>
<td>
@Html.EditorFor(modelItem => Model.TagActivities[i].AlertMinutes, new { htmlAttributes = new { @class = "form-control" } })
</td>
<td>
@Html.EditorFor(modelItem => Model.TagActivities[i].IsDefault)
</td>
</tr>
}
请帮助。谢谢。
答案 0 :(得分:0)
它不起作用,因为您传递的选定值与listItems
中的对象的类型不同。一旦可能的解决方案,就是将您传递给SelectList
的构造函数的值更改为所选对象:
new SelectList(listItems, "CategoryId", "CategoryName", Model.TagActivities[i].Category)
listItems
是Categories
类型的对象列表。因此,作为选定对象,您应该传递此类型的对象。