0

これは私のVMです

    public class CountriesViewModel
    {
        public string CurrentCountry { get; set; }
        public IEnumerable<Country> Countries { get; set; }
    }

私のコントローラーで

    public class FiltersController : Controller
    {
        public ActionResult _ShipToCountry(IEnumerable<Country> countries,
                                           string currentCountry = "AE")
        {
            var viewModel = new CountriesViewModel
            {
                Countries = countries.OrderBy(x => x.Name),
                CurrentCountry = currentCountry
            };

            return PartialView(viewModel);
        }
    }

そして最後にビュー

@Html.DropDownListFor(x => x.Countries,
                      new SelectList(Model.Countries.ToList(),
                                     "Code", "Name", Model.CurrentCountry))

ここで、Model.CurrentCountryはコードです。

DDLは正しくレンダリングされます

...
<option value="UA">Ukraine</option>
<option value="AE">United Arab Emirates</option>
<option value="UK">United Kingdom</option>
....

しかし、国は選択されません、私は何が欠けていますか?

4

2 に答える 2

1

CurrentCountryフォーム送信で選択した国を取得できない場合は、propをexpressionに設定する必要があります。

@Html.DropDownListFor(x => x.CurrentCountry,
                      new SelectList(Model.Countries.ToList(), "Code", "Name"))
于 2012-04-06T08:58:26.590 に答える
0

これはうまくいきました...

それ以外の

@Html.DropDownListFor(x => x.Countries,
                      new SelectList(Model.Countries.ToList(), 
                                     "Code", "Name", Model.CurrentCountry))

使用済み

@Html.DropDownList(Model.CurrentCountry,
                   new SelectList(Model.Countries.ToList(),
                                  "Code", "Name", Model.CurrentCountry))
于 2012-04-06T08:51:28.167 に答える