【发布时间】:2016-02-09 14:36:13
【问题描述】:
如何使用 HTML 助手编写下面的代码?
<select name="CountryId">
@foreach (var c in ViewBag.Counties) {
<option value="@c.Id">@c.Name</option>
}
</select>
上面的代码将在我的浏览器中提供正确的 html 代码。但是,如果我在选项标签中使用 value 属性 下方的 HTML 帮助器,则会丢失。
@Html.DropDownListFor(
model => model.CountryId,
new SelectList(ViewBag.Countries, "Name"),
new { htmlAttributes = new { @class = "form-control" } }
)
如果您知道ViewBag.Counties 是来自List<Country> 类型的对象并且具有Name(字符串类型)和CounrtyId(int 类型)属性,那么这段代码有什么问题。
【问题讨论】:
标签: asp.net-mvc razor html.dropdownlistfor