binding dropdownlist in mvc3 Razor(default Dropdownlist value is not selected)
I am binding dropdownlist in mvc3 Razor. My problem is its not showing default Dropdownlist value selected.An Empty space is shown. Code is given below:
@Html.DropDownListFor(model => model.MainHeadingFont, new SelectList(Model.lst_FontType, "Value", "Text"), new { @class = "inputPage7Select" })
CollectionList is passed from Model:
public List<SelectListItem> lst_FontType
{
get
{
FontType.RemoveRa开发者_Python百科nge(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;
}
}
In the controller action rendering this view:
public ActionResult Index()
{
var model = ...
model.MainHeadingFont = "\"Palatino Linotype\", Times, serif";
return View(model);
}
This will automatically preselect the second element of the ddl.
精彩评论