2012-04-05 61 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))