0

こんにちは、mvc5 でドロップダウン リストを作成するのに問題があります。これは私のモデルにあるものです。

public string CountryCode { get; set; }
public LocationChecker()
{
    PopulateCountryCodes();
}

public IEnumerable<SelectListItem> CountryCodes { get; set; }

private void PopulateCountryCodes()
{
    CountryCodes = new List<SelectListItem>();
    CountryCodes.Add (new SelectListItem {Value = "1", Text =“UK”});

    CountryCodes.Add (new SelectListItem {Value = "2", Text =“GB”});

    CountryCodes.Add (new SelectListItem {Value = "3", Text =“IRL”});
}

これが私の見解です:

<h4>Countries</h4>
  add a drop down list

  @Html.DropDownListFor(m => m.CountryCode, Model.CountryCodes)
</div>

CountryCodes.Add の ADD は赤で解決できません。また、UK、GB、IRL の直前に「式が必要です」というエラーがあります。

助けていただければ幸いです。mvc5 btwを使用するのはこれが初めてです。

ありがとう

ナビ

4

2 に答える 2

0

バックエンドで、SelectList を作成します

countryCodes = new List<SelectListItem>();
countryCodes.Add(new SelectListItem() { Text = "theText", Value = "TheValue" });

ビューモデルに割り当てます(これはコンストラクターにあるため、「これ」)

this.CountryCodes = new SelectList(countryCodes, "Value", "Text", "Please Select");

そしてフロントエンドで(CountryCodeはintであり、ビューモデルのメンバーです)

@Html.DropDownListFor(model => model.CountryCode, this.Model.CountryCodes, new { placeholder = "Please select a Country Code", @class = "form-control", name = "CountryCodeSelected" })
于 2015-10-22T08:25:05.513 に答える