您可以尝试MyFilter
在您的设置属性ViewBag/ViewData
:
public ActionResult Index()
{
ViewBag.SelectItems = new[]
{
new SelectListItem { Value = "1", Text = "item 1" },
new SelectListItem { Value = "2", Text = "item 2" },
new SelectListItem { Value = "3", Text = "item 3" },
};
// preselect the second item
ViewBag.MyFilter = "2";
return View();
}
但我的建议是使用视图模型和强类型版本的助手:
public ActionResult Index()
{
var model = new MyViewModel
{
SelectItems = new[]
{
new SelectListItem { Value = "1", Text = "item 1" },
new SelectListItem { Value = "2", Text = "item 2" },
new SelectListItem { Value = "3", Text = "item 3" },
},
// preselect the second item
MyFilter = "2"
};
return View(model);
}
在您的强类型视图中:
@model MyViewModel
...
@Html.DropDownListFor(x => x.MyFilter, Model.SelectItems, "Show all")