作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
Controller :
ViewBag.Category = new SelectList(this.service.GetAllCategories(), product.Category);
@Html.DropDownList("Category", String.Empty)
<select id="Category" name="Category">
<option value=""></option>
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option selected="selected">5</option>
...
</select>
@Html.DropDownList("Category", (IEnumerable<SelectListItem>)ViewBag.Category, new { @class = "anything" })
<select class="anything" id="Category" name="Category">
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
...
</select>
public List<int> GetAllCategories()
{
List<int> categories = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
return categories;
}
最佳答案
Html.DropDownList
作品很有趣:
如果您不提供选择列表
@Html.DropDownList("Category", String.Empty)
ViewData/ViewBag
中查找选择的值基于提供的属性名称:
Category
.
ViewData/ViewBag
中寻找默认选择的项目。基于提供的属性名称
Category
这当然将包含列表而不是默认值。要解决此问题,您有两个选择:
@Html.DropDownList("Category", null, new { @class = "anything" })
ViewBag
属性(property)名称
Category
:
@Html.DropDownList("CategoryDD", (IEnumerable<SelectListItem>)ViewBag.Category, new { @class = "anything" })
关于asp.net - 编辑不适用于 Html.DropDownList 和 @class 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8098361/
我是一名优秀的程序员,十分优秀!