I load my select lists in a controller. So it comes onto the page as part of the viewmodel.
The following will load the drop down with a css class clname and the first option will be "-- Select --"
@Html.DropDownListFor(model => model.param,
Model.MySelectList,
"-- Select --",
new { @class = "clname"})
Also, can I set a value to the default text select so that its value
will not be an empty string?
For this, you should load the select list in the controller with the appropriate values.
ViewModel:
public class HomeViewModel
{
public string MyParam {get;set;}
public List<SelectListItem> MySelectList {get;set;}
}
Controller:
public class HomeController
{
public ActionResult Index()
{
var model = new HomeViewModel();
// to load the list, you could put a function in a repository or just load it in your viewmodel constructor if it remains the same.
model.MySelectList = repository.LoadMyList();
model.MyParam = "Select"; // This will be the selected item in the list.
return View(model);
}
}
View:
@model MyProject.HomeViewModel
<p>Select:
@Html.DropDownListFor(model => model.MyParam,
Model.MySelectList,
new { @class = "clname"})
</p>
Hope that makes it clear.