asp.net-mvc – 使用带有剃刀的html选择框

前端之家收集整理的这篇文章主要介绍了asp.net-mvc – 使用带有剃刀的html选择框前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个html选择器,我想在我的表单中使用我的“model => model.type”的选定值.有没有办法将我的@ 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>

解决方法

您可以尝试使用以下选项:

模型:

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"}
        };
    }
}

HTML(Razor):

@Html.DropDownListFor(model => model.Type,Model.TypeList)

要么

HTML(Razor):

@Html.DropDownListFor(model => model.Type,new SelectList(new string[] {"Football","Rugby","Horse Racing"},Model.Type))

猜你在找的asp.Net相关文章