DropDownListFor 的 Razor 语法
Razor syntax for DropDownListFor
如何使用 HTML 助手编写下面的代码?
<select name="CountryId">
@foreach (var c in ViewBag.Counties) {
<option value="@c.Id">@c.Name</option>
}
</select>
以上代码将在我的浏览器中给出正确的 html 代码。但是,如果我在 值属性 下面使用选项标签中的 HTML 助手,则会丢失。
@Html.DropDownListFor(
model => model.CountryId,
new SelectList(ViewBag.Countries, "Name"),
new { htmlAttributes = new { @class = "form-control" } }
)
如果您知道 ViewBag.Counties
是类型 List<Country>
的对象并且具有属性 Name
(字符串类型)和 CounrtyId
( int 类型)。
尝试this.your 选择列表项class 应该在父class 中创建,即您在视图页面中绑定的模型class。
@Html.DropDownListFor(model => model.CountryId,new SelectList(ViewBag.Countries,"CountryID", "CountryName"),new {@class='form-control'})
如何使用 HTML 助手编写下面的代码?
<select name="CountryId">
@foreach (var c in ViewBag.Counties) {
<option value="@c.Id">@c.Name</option>
}
</select>
以上代码将在我的浏览器中给出正确的 html 代码。但是,如果我在 值属性 下面使用选项标签中的 HTML 助手,则会丢失。
@Html.DropDownListFor(
model => model.CountryId,
new SelectList(ViewBag.Countries, "Name"),
new { htmlAttributes = new { @class = "form-control" } }
)
如果您知道 ViewBag.Counties
是类型 List<Country>
的对象并且具有属性 Name
(字符串类型)和 CounrtyId
( int 类型)。
尝试this.your 选择列表项class 应该在父class 中创建,即您在视图页面中绑定的模型class。
@Html.DropDownListFor(model => model.CountryId,new SelectList(ViewBag.Countries,"CountryID", "CountryName"),new {@class='form-control'})