如何以更优雅的方式基于web.config确定MVC下拉列表中的选定值

时间:2014-02-07 16:15:05

标签: c# asp.net-mvc-4

我的视图中有一个下拉列表如下:

@Html.DropDownList("ServerListSL", ViewData["ServerList"] as SelectList, new { @class="form-
    control", id="chosen-server-name"})

现在我根据Web.Config文件中的值获取所选值,如下所示:

public ActionResult Index()
    {
        List<SelectListItem> serverItems = new List<SelectListItem>();

        if (@System.Configuration.ConfigurationManager.AppSettings["server:serverName"].ToString() == "P")
        {
            serverItems.Add(new SelectListItem { Text = "P", Value = "P", Selected = true   });
            serverItems.Add(new SelectListItem { Text = "A1", Value = "A1"                  });
            serverItems.Add(new SelectListItem { Text = "A2", Value = "A2"                  });
            serverItems.Add(new SelectListItem { Text = "T1", Value = "T1"                  });
            serverItems.Add(new SelectListItem { Text = "T2", Value = "T2"                  });
        }
        else if (@System.Configuration.ConfigurationManager.AppSettings["server:serverName"].ToString() == "A1")
        {
            serverItems.Add(new SelectListItem { Text = "P", Value = "P"                    });
            serverItems.Add(new SelectListItem { Text = "A1", Value = "A1", Selected = true });
            serverItems.Add(new SelectListItem { Text = "A2", Value = "A2"                  });
            serverItems.Add(new SelectListItem { Text = "T1", Value = "T1"                  });
            serverItems.Add(new SelectListItem { Text = "T2", Value = "T2"                  });
        }
        else if (@System.Configuration.ConfigurationManager.AppSettings["server:serverName"].ToString() == "A2")
        {
            serverItems.Add(new SelectListItem { Text = "P", Value = "P"                    });
            serverItems.Add(new SelectListItem { Text = "A1", Value = "A1"                  });
            serverItems.Add(new SelectListItem { Text = "A2", Value = "A2", Selected = true });
            serverItems.Add(new SelectListItem { Text = "T1", Value = "T1"                  });
            serverItems.Add(new SelectListItem { Text = "T2", Value = "T2"                  });
        }
        else if (@System.Configuration.ConfigurationManager.AppSettings["server:serverName"].ToString() == "T1")
        {
            serverItems.Add(new SelectListItem { Text = "P", Value = "P"                    });
            serverItems.Add(new SelectListItem { Text = "A1", Value = "A1"                  });
            serverItems.Add(new SelectListItem { Text = "A2", Value = "A2"                  });
            serverItems.Add(new SelectListItem { Text = "T1", Value = "T1", Selected = true });
            serverItems.Add(new SelectListItem { Text = "T2", Value = "T2"                  });
        }
        else if (@System.Configuration.ConfigurationManager.AppSettings["server:serverName"].ToString() == "T2")
        {
            serverItems.Add(new SelectListItem { Text = "P", Value = "P"                    });
            serverItems.Add(new SelectListItem { Text = "A1", Value = "A1"                  });
            serverItems.Add(new SelectListItem { Text = "A2", Value = "A2"                  });
            serverItems.Add(new SelectListItem { Text = "T1", Value = "T1"                  });
            serverItems.Add(new SelectListItem { Text = "T2", Value = "T2", Selected = true });
        }

        ViewData["ServerList"] = serverItems;

        return View();
    }

我怀疑这可以更优雅地完成。有什么想法吗?

1 个答案:

答案 0 :(得分:1)

List<SelectListItem> serverItems = new List<SelectListItem>();
            serverItems.Add(new SelectListItem { Text = "P", Value = "P" });
            serverItems.Add(new SelectListItem { Text = "A1", Value = "A1" });
            serverItems.Add(new SelectListItem { Text = "A2", Value = "A2" });
            serverItems.Add(new SelectListItem { Text = "T1", Value = "T1" });
            serverItems.Add(new SelectListItem { Text = "T2", Value = "T2" });

            string selectedValue = System.Configuration.ConfigurationManager.AppSettings["server:serverName"].ToString();

            SelectListItem item = serverItems.Where(t => t.Value == selectedValue).SingleOrDefault();

            if (item != null)
            {
                item.Selected = true;
            }