2011-04-15 79 views
0

我綁定在mvc3 Razor中的dropdownlist。 我的問題是它沒有顯示默認的Dropdownlist值selected.An空格顯示。下面 代碼給出:在mvc3 Razor中綁定dropdownlist

@Html.DropDownListFor(model => model.MainHeadingFont, new SelectList(Model.lst_FontType, "Value", "Text"), new { @class = "inputPage7Select" }) 

CollectionList從模型傳遞:

public List<SelectListItem> lst_FontType 
    { 
     get 
     { 
      FontType.RemoveRange(0, FontType.Count); 
      FontType.Add(new SelectListItem() { Text = "\"Gill Sans MT\", Arial, sans-serif", Value = "\"Gill Sans MT\", Arial, sans-serif", Selected = true }); 
      FontType.Add(new SelectListItem() { Text = "\"Palatino Linotype\", Times, serif", Value = "\"Palatino Linotype\", Times, serif" }); 
      FontType.Add(new SelectListItem() { Text = "\"Times New Roman\", Times, serif", Value = "\"Times New Roman\", Times, serif" }); 
      return FontType; 
     } 
    } 

回答

1

在控制器動作呈現這樣的觀點:

public ActionResult Index() 
{ 
    var model = ... 
    model.MainHeadingFont = "\"Palatino Linotype\", Times, serif"; 
    return View(model); 
} 

這將自動預選的第二元件DDL。