这是我的VM不能得到MVC3 DDL的SelectedValue工作
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>
....
渲染,但没有一个国家被选中,我缺少什么?
@kooshka ...看起来没错。你确定“AE”在国家的“代码”列表中吗 – MikeTWebb 2012-04-05 21:11:50
“AE”只是它在列表中的默认值。但是任何其他国家的代码是通过的,并且通过调试时我可以看到它,仍然不选择DDL – kooshka 2012-04-05 21:14:32