0

这是我的虚拟机

    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

如果不是,您应该将CurrentCountryprop 设置为表达式,否则您无法在表单提交时获得所选国家/地区。

@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 回答