0

我有一个表单和 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")

提前致谢。

4

2 回答 2

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")
于 2013-08-04T20:16:43.843 回答
0

你不能。除非在提交之后,您将模型传回视图。你为什么要这样做?因为在表单提交之后,表单应该重置并为另一个数据输入会话做好准备,不是吗?

于 2013-08-05T02:42:58.737 回答