在mvc中提交表单后保存下拉列表状态

时间:2013-08-04 20:02:40

标签: asp.net-mvc asp.net-mvc-4 razor

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

提前致谢。

2 个答案:

答案 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)

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