2013-05-27 139 views
3

我有這個模型MVC 4剃刀的DropDownList

public int id { get; set; } 
public string Status { get; set; } 

我需要status有3個可能的值只有{Waiting, Approved, Rejected}

我想用一個強類型的視圖。那麼最好的辦法是什麼?我將使用

<select name="Status" id="Status"> 
      <option value="Waiting">Waiting</option> 
      <option value="Approved">Approved</option> 
      <option value="Rejected">Rejected</option> 
</select> 

我想用Enumstatus最好但無論如何,這樣做會被罰款的。

+1

[此](http://stackoverflow.com/questions/388483/how-do-you-create-a-dropdownlist- from-an-enum-in-asp-net-mvc)將幫助你使用'enum' – Yoav

+0

我想你要麼創建查找表,要麼創建一個@Max Zhukov在你下面建議的集合。 – Rajpurohit

+0

[DropDown綁定在MVC 4 Razor視圖中](http://lesson8.blogspot.com/2013/06/bind-dropdownlist-in-mvc4-razor.html) – Sender

回答

0
@{var listItems = new List<ListItem> 
{ 
     new ListItem { Text = "Waiting", Value="Waiting" }, 
     new ListItem { Text = "Approved", Value="Approved" }, 
     new ListItem { Text = "Rejected", Value="Rejected" } 
}; 
} 
    @Html.DropDownList("Approved",new SelectList(listItems,"Value","Text")) 
0

模型

public enum Status 
{ 
Waiting = 1, 
Approved = 2, 
Approved = 3 
} 

public Status Status { get; set; } 

public IEnumerable GetStatus 
{ 
get 
{ 
return 
Enum.GetValues(typeof(Status)).Cast().Select(p => new SelectListItem 
{ 
Text = p.ToString(), 
Value = p.ToString() 
}).ToList(); 
} 
} 

查看

@Html.DropDownListFor(model => model.Status,Model.GetStatus, 「Select Status」) 
+0

僅提供代碼答案已棄用。請添加一些評論 –