这是我的虚拟机
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>
....
但是没有国家被选中,我错过了什么?