我有一个html选择器,我想在我的表单中使用我的"model => model.type"的选定值.有没有办法将my @Html.EditorFor(model => model.type)中的值设置为选择器的值?
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>Bet</legend>
<div class="editor-label">
@Html.LabelFor(model => model.type)
</div>
<div class="editor-field">
<select id ="type">
<option value="Football">Football</option>
<option value="Rugby">Rugby</option>
<option value="Horse Racing">Horse Racing</option>
</select>
@Html.EditorFor(model => model.type)
@Html.ValidationMessageFor(model => model.type)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
Run Code Online (Sandbox Code Playgroud)
and*_*lzo 20
您可以尝试使用以下选项:
模型:
public string Type { get; set; }
public IEnumerable<SelectListItem> TypeList
{
get
{
return new List<SelectListItem>
{
new SelectListItem { Text = "Football", Value = "Football"},
new SelectListItem { Text = "Rugby", Value = "Rugby"},
new SelectListItem { Text = "Horse Racing", Value = "Horse Racing"}
};
}
}
Run Code Online (Sandbox Code Playgroud)
HTML(Razor):
@Html.DropDownListFor(model => model.Type, Model.TypeList)
Run Code Online (Sandbox Code Playgroud)
要么
HTML(Razor):
@Html.DropDownListFor(model => model.Type, new SelectList(new string[] {"Football", "Rugby", "Horse Racing"}, Model.Type))
Run Code Online (Sandbox Code Playgroud)
@andresdescalzo 的解决方案(最后一个)只需稍作改动即可:
@Html.DropDownListFor(model => model.Type, new SelectList(new string[] {"Football", "Rugby", "Horse Racing"}, "Rugby"), htmlAttributes: new { @class = "form-control" })
Run Code Online (Sandbox Code Playgroud)
第一:为下拉列表添加选定的项目(例如“橄榄球”)
第二:删除最后一个Model.Type并添加htmlAttributes
PS:SelectedList 在列表的选定项目之后关闭括号(这里是“ Rugby”)
| 归档时间: |
|
| 查看次数: |
41931 次 |
| 最近记录: |