加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

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

发布时间:2020-12-15 22:27:28 所属栏目:asp.Net 来源:网络整理
导读:我有一个html选择器,我想在我的表单中使用我的“model = model.type”的选定值.有没有办法将我的@ Html.EditorFor(model = model.type)中的值设置为选择器的值? @using (Html.BeginForm()) {@Html.ValidationSummary(true)fieldset legendBet/legend div cl
我有一个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))

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读