11

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

5 に答える 5

23

このオプションを試すことができます:

モデル:

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 (カミソリ):

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

また

HTML (カミソリ):

@Html.DropDownListFor(model => model.Type, new SelectList(new string[] {"Football", "Rugby", "Horse Racing"}, Model.Type))
于 2012-12-14T21:19:43.800 に答える