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))

相关文章

### 创建一个gRPC服务项目(grpc服务端)和一个 webapi项目(...
一、SiganlR 使用的协议类型 1.websocket即时通讯协议 2.Ser...
.Net 6 WebApi 项目 在Linux系统上 打包成Docker镜像,发布为...
一、 PD简介PowerDesigner 是一个集所有现代建模技术于一身的...
一、存储过程 存储过程就像数据库中运行的方法(函数) 优点:...
一、Ueditor的下载 1、百度编辑器下载地址:http://ueditor....