微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

原文地址:https://www.jb51.cc/aspnet/249184.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐