0
的web.config我有一個下拉列表中我的觀點如下:如何確定MVC選定值下拉列表基礎上更優雅的方式
@Html.DropDownList("ServerListSL", ViewData["ServerList"] as SelectList, new { @class="form-
control", id="chosen-server-name"})
現在我把基於網絡的值的選定值配置文件是這樣的:
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();
}
我有一個強烈的懷疑,這可以做得更優雅。有任何想法嗎?