3

I have this model

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

I need the status to have 3 possible values only {Waiting, Approved, Rejected}

I want to use a strongly typed view. So what's the best way to do it? Shall I use

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

I would like to use Enum for status preferably but anyway of doing it would be fine.

tereško
  • 58,060
  • 25
  • 98
  • 150
RonaDona
  • 912
  • 6
  • 13
  • 30
  • 1
    [this](http://stackoverflow.com/questions/388483/how-do-you-create-a-dropdownlist-from-an-enum-in-asp-net-mvc) will help you use an `enum` – Yoav May 27 '13 at 05:08
  • i think either you have to create lookup table or you create a collection which is @Max Zhukov suggest below you . – Rajpurohit May 27 '13 at 06:03
  • [DropDown Bind in MVC 4 Razor View](http://lesson8.blogspot.com/2013/06/bind-dropdownlist-in-mvc4-razor.html) – Sender Oct 03 '13 at 16:07
  • Add items to your DropDownList from code-behind like [this](http://odetocode.com/blogs/scott/archive/2013/03/11/dropdownlistfor-with-asp-net-mvc.aspx) – Max Zhukov May 27 '13 at 04:42

2 Answers2

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"))
john Peralta
  • 771
  • 5
  • 3
0

Model

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();
}
}

View

@Html.DropDownListFor(model => model.Status,Model.GetStatus, “Select Status”)
Nirav Parmar
  • 451
  • 3
  • 12