When using DropDownListFor items from being selected during the initial display of the drop-down list for the value of the property specified in the lambda expression is not used.
Because the value in the ViewData is used after POST, the item is selected correctly.
The sample code is as follows:
```
public ActionResult Index()
{
// create SelectList (sample values)
ViewBag.Category = new SelectList(Enumerable.Range(1, 5));
return View(new FormModel
{
// set default value
Category = 3,
});
}
```
```
@* use expression helper, "3" item is not selected. *@
@Html.DropDownListFor(model => model.Category, null)
```
Take a look at my blog. It is with screenshots.
[When combined with ViewBag and DropDownListFor problem](http://www.microsofttranslator.com/bv.aspx?from=&to=en&a=http://shiba-yan.hatenablog.jp/entry/20130207/1360226954)
Because the value in the ViewData is used after POST, the item is selected correctly.
The sample code is as follows:
```
public ActionResult Index()
{
// create SelectList (sample values)
ViewBag.Category = new SelectList(Enumerable.Range(1, 5));
return View(new FormModel
{
// set default value
Category = 3,
});
}
```
```
@* use expression helper, "3" item is not selected. *@
@Html.DropDownListFor(model => model.Category, null)
```
Take a look at my blog. It is with screenshots.
[When combined with ViewBag and DropDownListFor problem](http://www.microsofttranslator.com/bv.aspx?from=&to=en&a=http://shiba-yan.hatenablog.jp/entry/20130207/1360226954)