我有一个表单和Html.DropDownList。表单提交后,下拉列表状态将更改为默认值。如何在表单提交后保持下拉列表状态?
@using (Html.BeginForm("Result", "Controller", FormMethod.Post, new { id = "Form" }))
{
@Html.DropDownListFor(x => x.DropList, new[] {
new SelectListItem() {Text = "first", Value = "first"},
new SelectListItem() {Text = "second", Value = "second"},
new SelectListItem() {Text = "third", Value = "third"}
}, "DefaultState")
提前致谢。
答案 0 :(得分:0)
使用SelectListItem的Selected属性
另外,请确保从帖子中在控制器中设置了Model.DropList的值。
@using (Html.BeginForm("List", "Home", FormMethod.Post, new { id = "Form" }))
{
@Html.DropDownListFor(x => x.DropList, new[] {
new SelectListItem() {Text = "first", Value = "first", Selected = Model.DropList == "first"},
new SelectListItem() {Text = "second", Value = "second", Selected = Model.DropList == "second"},
new SelectListItem() {Text = "third", Value = "third", Selected = Model.DropList == "third"}
}, "DefaultState")
答案 1 :(得分:0)