2012-04-05 60 views
0

这是我的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> 
.... 

渲染,但没有一个国家被选中,我缺少什么?

+1

@kooshka ...看起来没错。你确定“AE”在国家的“代码”列表中吗 – MikeTWebb 2012-04-05 21:11:50

+0

“AE”只是它在列表中的默认值。但是任何其他国家的代码是通过的,并且通过调试时我可以看到它,仍然不选择DDL – kooshka 2012-04-05 21:14:32

回答

1

您应该设置CurrentCountry道具表达如果不还你不能得到所选国家的形式提交。

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

你是对的,谢谢! – kooshka 2012-04-06 09:02:47

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))